UserController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. <?php
  2. namespace mall\controllers;
  3. use biz\shop\classes\ShopExtClass;
  4. use bizHd\custom\classes\CustomClass;
  5. use bizHd\wx\classes\WxOpenClass;
  6. use bizMall\message\services\SmsService;
  7. use bizMall\user\classes\UserClass;
  8. use bizMall\user\classes\UserGrowthClass;
  9. use bizMall\user\classes\UserIntegralClass;
  10. use bizMall\user\services\UserAssetService;
  11. use bizMall\user\services\UserService;
  12. use biz\shop\classes\ShopClass;
  13. use common\components\dict;
  14. use common\components\imgUtil;
  15. use common\components\jwt;
  16. use common\components\noticeUtil;
  17. use common\components\stringUtil;
  18. use Yii;
  19. use common\components\util;
  20. use yii\helpers\Json;
  21. use Overtrue\ChineseCalendar\Calendar;
  22. class UserController extends BaseController
  23. {
  24. //二维码收款使用 ssh
  25. public $guestAccess = ['discount', 'mini-mobile', 'create', 'account-login', 'current-info'];
  26. //清空登录信息 ssh 20250215
  27. public function actionClearLogin()
  28. {
  29. $user = $this->user;
  30. $user->miniOpenId = '';
  31. $user->save();
  32. util::complete();
  33. }
  34. //修改信息 ssh 20221015
  35. public function actionUpdate()
  36. {
  37. $post = Yii::$app->request->post();
  38. $user = $this->user;
  39. if (empty($user)) {
  40. util::fail('请先登录');
  41. }
  42. $userId = $user->id ?? 0;
  43. $address = $post['address'] ?? '';
  44. $floor = $post['floor'] ?? '';
  45. $fullAddress = $address . $floor;
  46. $post['fullAddress'] = $fullAddress;
  47. UserClass::updateUserInfo($user, $post);
  48. util::complete();
  49. }
  50. //修改生日 ssh 20250830
  51. public function actionModifyBirthday()
  52. {
  53. date_default_timezone_set('PRC');
  54. $get = Yii::$app->request->get();
  55. $lunar = $get['lunar'] ?? 0;
  56. $birthdayMonth = $get['birthdayMonth'] ?? 1;
  57. if ($birthdayMonth > 12 || $birthdayMonth < 1) {
  58. util::fail('月份必须1到12月');
  59. }
  60. $birthdayDate = $get['birthdayDate'] ?? 1;
  61. if ($birthdayDate > 31 || $birthdayDate < 1) {
  62. util::fail('日期必须1到31号');
  63. }
  64. if ($lunar == 1 && $birthdayDate == 31) {
  65. util::fail('农历没有31号');
  66. }
  67. $user = $this->user;
  68. if ($user->birthdaySet == 1) {
  69. util::fail('你已经设置过了');
  70. }
  71. if ($lunar == 1) {
  72. try {
  73. //农历转阳历,有多处使用,需要同步修改,关键词 change_my_birthday
  74. $calendar = new Calendar();
  75. $result = $calendar->lunar(date("Y"), $birthdayMonth, $birthdayDate);
  76. $month = $result['gregorian_month'] ?? 1;
  77. $date = $result['gregorian_day'] ?? 1;
  78. $birthday = date("Y") . '-' . $month . '-' . $date;
  79. } catch (\InvalidArgumentException $e) {
  80. $msg = $e->getMessage();
  81. $birthday = '0000-00-00 00:00:00';
  82. util::fail($msg);
  83. }
  84. } else {
  85. $birthday = date("Y") . '-' . $birthdayMonth . '-' . $birthdayDate;
  86. }
  87. $birthdayTime = strtotime($birthday);
  88. $user->lunar = $lunar;
  89. $user->birthdayDate = $birthdayDate;
  90. $user->birthdayMonth = $birthdayMonth;
  91. $user->birthday = $birthday;
  92. $user->birthdayTime = $birthdayTime;
  93. $user->birthdaySet = 1;
  94. $user->save();
  95. $userId = $user->id;
  96. $customList = \bizMall\custom\classes\CustomClass::getAllByCondition(['userId' => $userId], null, '*', null, true);
  97. if (!empty($customList)) {
  98. foreach ($customList as $custom) {
  99. $custom->lunar = $lunar;
  100. $custom->birthdayDate = $birthdayDate;
  101. $custom->birthdayMonth = $birthdayMonth;
  102. $custom->birthday = $birthday;
  103. $custom->birthdayTime = $birthdayTime;
  104. $custom->save();
  105. }
  106. }
  107. util::complete('修改成功');
  108. }
  109. //获取当前登录用户的信息 ssh 20221014
  110. public function actionCurrentInfo()
  111. {
  112. $user = $this->user;
  113. if (!empty($user)) {
  114. $birthday = $user->birthday ?? '';
  115. //如果当年的生日时间没有更新,则需要给更新
  116. $year = date("Y", strtotime($birthday));
  117. $thisYear = date("Y");
  118. if ($year != $thisYear) {
  119. $lunar = $user->lunar ?? 0;
  120. $userId = $this->userId;
  121. $birthdayMonth = $user->birthdayMonth ?? 0;
  122. $birthdayDate = $user->birthdayDate ?? 0;
  123. if ($lunar == 1 && $birthdayMonth > 0 && $birthdayDate > 0) {
  124. try {
  125. //农历转阳历,有多处使用,需要同步修改,关键词change_my_birthday
  126. $calendar = new Calendar();
  127. $result = $calendar->lunar(date("Y"), $birthdayMonth, $birthdayDate);
  128. $month = $result['gregorian_month'] ?? 1;
  129. $date = $result['gregorian_day'] ?? 1;
  130. $birthday = date("Y") . '-' . $month . '-' . $date;
  131. $user->birthday = $birthday;
  132. $user->save();
  133. CustomClass::updateByCondition(['userId' => $userId], ['birthday' => $birthday]);
  134. } catch (\InvalidArgumentException $e) {
  135. $msg = $e->getMessage();
  136. noticeUtil::push("更新生日出错了呢,userId:" . $userId . ' ' . $msg, '15280215347');
  137. }
  138. } else {
  139. $birthday = date("Y") . '-' . $birthdayMonth . '-' . $birthdayDate;
  140. $user->birthday = $birthday;
  141. $user->save();
  142. CustomClass::updateByCondition(['userId' => $userId], ['birthday' => $birthday]);
  143. }
  144. }
  145. // 将 $user 转换为数组,然后添加 hasMap 属性(字典中的 hasMap)
  146. $user = $user->toArray();
  147. $shortAvatar = $user['avatar'] ?? '';
  148. $smallAvatar = imgUtil::groupImg($shortAvatar) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  149. $avatar = imgUtil::groupImg($shortAvatar) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  150. $user['avatar'] = $avatar;
  151. $user['smallAvatar'] = $smallAvatar;
  152. $user['shortAvatar'] = $shortAvatar;
  153. }
  154. // 补充店铺信息
  155. $shop = [];
  156. if (!empty($this->shop)) {
  157. $intraCityRet = ShopClass::hasIntraCity($this->shop);
  158. $shop['openIntraCity'] = $intraCityRet['openIntraCity'];
  159. $shop['hcFreeKm'] = $intraCityRet['hcFreeKm'];
  160. $shop['hcMap'] = $intraCityRet['hcMap'];
  161. }
  162. util::success(['info' => $user, 'shop' => $shop]);
  163. }
  164. //使用账号登录 ssh 20220920
  165. public function actionAccountLogin()
  166. {
  167. $getParams = Yii::$app->request->get();
  168. $postParams = Yii::$app->request->post();
  169. $allParams = array_merge($getParams, $postParams);
  170. $mobile = $allParams['mobile'] ?? '';
  171. $password = $allParams['password'] ?? '';
  172. if ($password != 'a.1234') {
  173. util::fail('密码错误');
  174. }
  175. $user = UserClass::getByCondition(['mobile' => $mobile], true);
  176. if (empty($user)) {
  177. util::fail('账号无效');
  178. }
  179. $userId = $user->id ?? 0;
  180. $mallUpgrading = getenv('MALL_UPGRADING') == false ? 0 : getenv('MALL_UPGRADING');
  181. if ($mallUpgrading == 1) {
  182. $allowShopAdminIdsStr = getenv('MALL_UPGRADE_ALLOW_USER_IDS') ?: '';
  183. $allowShopAdminIds = !empty($allowShopAdminIdsStr) ? explode(',', $allowShopAdminIdsStr) : [];
  184. if (!in_array($userId, $allowShopAdminIds)) {
  185. util::fail('系统升级中,稍后再试,编号'.$userId);
  186. }
  187. }
  188. $token = !empty($userId) ? jwt::getNewToken($userId) : '';
  189. // 将 $user 转换为数组,然后添加 hasMap 属性(字典中的 hasMap)
  190. $user = $user->toArray();
  191. $user['hasMap'] = dict::getDict('hasMap');
  192. util::success(['user' => $user, 'token' => $token]);
  193. }
  194. //生成用户和花店增加客户 ssh 20250307
  195. public function actionCreate()
  196. {
  197. $post = Yii::$app->request->post();
  198. $user = UserClass::replaceUser($post);
  199. if (!empty($this->shop)) {
  200. //花店增加客户
  201. CustomClass::buildRelation($this->shop, $user);
  202. }
  203. $userId = $user->id ?? 0;
  204. $token = !empty($userId) ? jwt::getNewToken($userId) : '';
  205. $fullUser = UserClass::getById($userId, true);
  206. // 将 $fullUser 转换为数组,然后添加 hasMap 属性(字典中的 hasMap)
  207. $fullUser = $fullUser->toArray();
  208. $fullUser['hasMap'] = dict::getDict('hasMap');
  209. util::success(['user' => $fullUser, 'token' => $token]);
  210. }
  211. //最近店铺 ssh 2021.2.27
  212. public function actionRecentShop()
  213. {
  214. $list = [];
  215. //显示国兰的信息 0不是 1是
  216. $showGlInfo = getenv('SHOW_GL_INFO');
  217. $info = $showGlInfo === false ? 1 : $showGlInfo;
  218. util::success(['list' => $list, 'showGlInfo' => $info]);
  219. }
  220. //获取登陆客户的资产 ssh 2019.11.22
  221. public function actionAsset()
  222. {
  223. $asset = UserAssetService::getByUserId($this->userId);
  224. util::success($asset);
  225. }
  226. //获取登陆客户的优惠情况 ssh 2019.12.3
  227. public function actionDiscount()
  228. {
  229. $discount = UserAssetService::getDiscount($this->userId);
  230. util::success($discount);
  231. }
  232. //小程序用户完整信息 ssh 2019.12.12
  233. public function actionMiniFullInfo()
  234. {
  235. $post = Yii::$app->request->post();
  236. $iv = $post['iv'] ?? '';
  237. $encryptedData = $post['encryptedData'] ?? '';
  238. $merchant = WxOpenClass::getMallWxMiniBase();
  239. $appId = $merchant['miniAppId'];
  240. $miniOpenId = $post['miniOpenId'];
  241. if (empty($miniOpenId)) {
  242. util::success(['hasNoOpenId' => 1], '登录失败');
  243. }
  244. $cacheKey = 'MALL_MINI_SESSION_KEY_' . $miniOpenId;
  245. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  246. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  247. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  248. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  249. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  250. if ($errCode != 0) {
  251. Yii::info($result . ' ' . $errCode);
  252. util::fail('获取用户信息失败');
  253. }
  254. Yii::info('小程序获取用户信息:' . $result);
  255. $originalInfo = Json::decode($result);
  256. util::success($originalInfo);
  257. }
  258. //小程序用户手机号
  259. public function actionMiniMobile()
  260. {
  261. $post = Yii::$app->request->post();
  262. $iv = $post['iv'];
  263. $encryptedData = $post['encryptedData'];
  264. $merchant = WxOpenClass::getMallWxMiniBase();
  265. $appId = $merchant['miniAppId'];
  266. $miniOpenId = $post['miniOpenId'];
  267. if (empty($miniOpenId)) {
  268. util::success(['hasNoOpenId' => 1], '登录失败');
  269. }
  270. $cacheKey = 'MALL_MINI_SESSION_KEY_' . $miniOpenId;
  271. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  272. if (empty($sessionKey)) {
  273. util::fail('系统错误,session empty');
  274. }
  275. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  276. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  277. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  278. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  279. if ($errCode != 0) {
  280. util::fail('解密失败');
  281. }
  282. $arr = Json::decode($result);
  283. $mobile = $arr['purePhoneNumber'] ?? '';
  284. if (empty($mobile)) {
  285. util::fail('没有取到手机号,请重试');
  286. }
  287. util::success(['mobile' => $mobile]);
  288. }
  289. //密码修改
  290. public function actionPassword()
  291. {
  292. $post = Yii::$app->request->post();
  293. $old = $post['old'] ?? '';
  294. $new = $post['new'] ?? '';
  295. $confirm = $post['confirm'] ?? '';
  296. if (empty($new)) {
  297. util::fail('请输入新密码');
  298. }
  299. if ($new != $confirm) {
  300. util::fail('二次密码不一致');
  301. }
  302. $userId = $this->userId;
  303. $user = UserService::getUserInfo($userId);
  304. if (isset($user['hasPayPwd']) && $user['hasPayPwd'] == 1) {
  305. if (empty($old)) {
  306. util::fail('请填写旧密码');
  307. }
  308. if (!password_verify($old, $user['payPassword'])) {
  309. util::fail('旧密码错误');
  310. }
  311. }
  312. UserService::updateById($userId, ['payPassword' => password_hash($new, PASSWORD_BCRYPT), 'hasPayPwd' => 1]);
  313. util::complete('修改成功');
  314. }
  315. //申请会员 ssh 2019.12.18
  316. public function actionApplyMember()
  317. {
  318. $post = Yii::$app->request->post();
  319. $mobile = $post['mobile'] ?? 0;
  320. if (!stringUtil::isMobile($mobile)) {
  321. util::fail('请输入手机号');
  322. }
  323. $userId = $this->userId;
  324. $asset = UserAssetService::getByUserId($userId);
  325. if (isset($asset['member']) && $asset['member'] != -1) {
  326. util::fail('已经是会员了');
  327. }
  328. $code = $post['code'] ?? '';
  329. $cacheCode = SmsService::getApplyMemberCode($userId, $mobile);
  330. if (empty($cacheCode) || $cacheCode != $code) {
  331. util::fail('验证码错误');
  332. }
  333. $update = [];
  334. if (!empty($post['realName'])) {
  335. $update['realName'] = $post['realName'];
  336. }
  337. if (!empty($post['birthday'])) {
  338. $update['birthday'] = $post['birthday'];
  339. }
  340. $user = UserService::getByCondition(['sjId' => $this->sjId, 'mobile' => $mobile]);
  341. if ($userId == $user['id']) {
  342. util::fail('手机号已验证');
  343. }
  344. if (!empty($user)) {
  345. util::fail('手机号已经注册过了');
  346. }
  347. $update['mobile'] = $mobile;
  348. $userInfo = $this->user->attributes ?? [];
  349. UserService::becomeMember($userInfo, $this->sj->attributes, $update);
  350. util::complete('注册成功');
  351. }
  352. //登陆客户的信息 ssh 2020.3.12
  353. public function actionLoginDetail()
  354. {
  355. $user = $this->user->attributes ?? [];
  356. util::success($user);
  357. }
  358. //成长值列表
  359. public function actionGrowthList()
  360. {
  361. $where = [];
  362. $where['shopId'] = $this->shopId;
  363. $where['userId'] = $this->userId;
  364. $list = UserGrowthClass::getGrowthList($where);
  365. util::success($list);
  366. }
  367. //积分列表
  368. public function actionIntegralList()
  369. {
  370. $where = [];
  371. $where['shopId'] = $this->shopId;
  372. $where['userId'] = $this->userId;
  373. $list = UserIntegralClass::getIntegralList($where);
  374. util::success($list);
  375. }
  376. }