UserController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. <?php
  2. namespace mall\controllers;
  3. use bizHd\birthday\classes\BirthdayGiftClass;
  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', 'get-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 (getenv('YII_ENV') != 'dev' && $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. // 同步变更客户表(xhCustom)中的数据
  96. $customList = \bizMall\custom\classes\CustomClass::getAllByCondition(['userId' => $user->id], 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. BirthdayGiftClass::clearWorkbenchSummaryCache($custom->shopId);
  107. }
  108. }
  109. // 同步变更生日赠礼记录表(xhBirthdayGift)中的数据(只更新当年年份的,往年的不去修改)
  110. $birthdayGiftList = BirthdayGiftClass::getAllByCondition(['userId' => $user->id, 'year' => date("Y")], null, '*', null, true);
  111. if (!empty($birthdayGiftList)) {
  112. foreach ($birthdayGiftList as $birthdayGift) {
  113. list($giftBirthdayMonth, $giftBirthdayDate) = BirthdayGiftClass::convertBirthdayMonthDay(
  114. $lunar,
  115. $birthdayMonth,
  116. $birthdayDate
  117. );
  118. $birthdayGift->lunar = $lunar;
  119. $birthdayGift->birthdayMonth = $giftBirthdayMonth;
  120. $birthdayGift->birthdayDate = $giftBirthdayDate;
  121. $birthdayGift->birthdayDisplay = BirthdayGiftClass::formatBirthdayDisplay($custom, $giftBirthdayMonth, $giftBirthdayDate);
  122. $birthdayGift->status = 1;
  123. $birthdayGift->save();
  124. }
  125. }
  126. util::complete('修改成功');
  127. }
  128. //获取当前登录用户的信息 ssh 20221014
  129. public function actionCurrentInfo()
  130. {
  131. $user = $this->user;
  132. if (!empty($user)) {
  133. $user = $user->toArray();
  134. $shortAvatar = $user['avatar'] ?? '';
  135. $smallAvatar = imgUtil::groupImg($shortAvatar) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  136. $avatar = imgUtil::groupImg($shortAvatar) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  137. $user['avatar'] = $avatar;
  138. $user['smallAvatar'] = $smallAvatar;
  139. $user['shortAvatar'] = $shortAvatar;
  140. }
  141. // 补充店铺信息
  142. $shop = [];
  143. if (!empty($this->shop)) {
  144. $intraCityRet = ShopClass::hasIntraCity($this->shop);
  145. $shop['openIntraCity'] = $intraCityRet['openIntraCity'];
  146. $shop['hcFreeKm'] = $intraCityRet['hcFreeKm'];
  147. $shop['hcMap'] = $intraCityRet['hcMap'];
  148. }
  149. util::success(['info' => $user, 'shop' => $shop]);
  150. }
  151. //使用账号登录 ssh 20220920
  152. public function actionAccountLogin()
  153. {
  154. $getParams = Yii::$app->request->get();
  155. $postParams = Yii::$app->request->post();
  156. $allParams = array_merge($getParams, $postParams);
  157. $mobile = $allParams['mobile'] ?? '';
  158. $password = $allParams['password'] ?? '';
  159. if ($password != 'a.1234') {
  160. util::fail('密码错误');
  161. }
  162. $user = UserClass::getByCondition(['mobile' => $mobile], true);
  163. if (empty($user)) {
  164. util::fail('账号无效');
  165. }
  166. $userId = $user->id ?? 0;
  167. if ($userId > 0) {
  168. $mallUpgrading = getenv('MALL_UPGRADING') == false ? 0 : getenv('MALL_UPGRADING');
  169. if ($mallUpgrading == 1) {
  170. $allowShopAdminIdsStr = getenv('MALL_UPGRADE_ALLOW_USER_IDS') ?: '';
  171. $allowShopAdminIds = !empty($allowShopAdminIdsStr) ? explode(',', $allowShopAdminIdsStr) : [];
  172. if (!in_array($userId, $allowShopAdminIds)) {
  173. util::fail('系统升级中,稍后再试,编号:' . $userId);
  174. }
  175. }
  176. }
  177. $token = !empty($userId) ? jwt::getNewToken($userId) : '';
  178. // 将 $user 转换为数组,然后添加 hasMap 属性(字典中的 hasMap)
  179. $user = $user->toArray();
  180. $user['hasMap'] = dict::getDict('hasMap');
  181. util::success(['user' => $user, 'token' => $token]);
  182. }
  183. //生成用户和花店增加客户 ssh 20250307
  184. public function actionCreate()
  185. {
  186. $post = Yii::$app->request->post();
  187. $user = UserClass::replaceUser($post);
  188. if (!empty($this->shop)) {
  189. //花店增加客户
  190. CustomClass::buildRelation($this->shop, $user);
  191. }
  192. $userId = $user->id ?? 0;
  193. if ($userId > 0) {
  194. $mallUpgrading = getenv('MALL_UPGRADING') == false ? 0 : getenv('MALL_UPGRADING');
  195. if ($mallUpgrading == 1) {
  196. $allowShopAdminIdsStr = getenv('MALL_UPGRADE_ALLOW_USER_IDS') ?: '';
  197. $allowShopAdminIds = !empty($allowShopAdminIdsStr) ? explode(',', $allowShopAdminIdsStr) : [];
  198. if (!in_array($userId, $allowShopAdminIds)) {
  199. util::fail('系统升级中,稍后再试,编号:' . $userId);
  200. }
  201. }
  202. }
  203. $token = !empty($userId) ? jwt::getNewToken($userId) : '';
  204. $fullUser = UserClass::getById($userId, true);
  205. // 将 $fullUser 转换为数组,然后添加 hasMap 属性(字典中的 hasMap)
  206. $fullUser = $fullUser->toArray();
  207. $fullUser['hasMap'] = dict::getDict('hasMap');
  208. util::success(['user' => $fullUser, 'token' => $token]);
  209. }
  210. //最近店铺 ssh 2021.2.27
  211. public function actionRecentShop()
  212. {
  213. $list = [];
  214. //显示国兰的信息 0不是 1是
  215. $showGlInfo = getenv('SHOW_GL_INFO');
  216. $info = $showGlInfo === false ? 1 : $showGlInfo;
  217. util::success(['list' => $list, 'showGlInfo' => $info]);
  218. }
  219. //获取登陆客户的资产 ssh 2019.11.22
  220. public function actionAsset()
  221. {
  222. $asset = UserAssetService::getByUserId($this->userId);
  223. util::success($asset);
  224. }
  225. //获取登陆客户的优惠情况 ssh 2019.12.3
  226. public function actionDiscount()
  227. {
  228. $discount = UserAssetService::getDiscount($this->userId);
  229. util::success($discount);
  230. }
  231. //小程序用户完整信息 ssh 2019.12.12
  232. public function actionMiniFullInfo()
  233. {
  234. $post = Yii::$app->request->post();
  235. $iv = $post['iv'] ?? '';
  236. $encryptedData = $post['encryptedData'] ?? '';
  237. $merchant = WxOpenClass::getMallWxMiniBase();
  238. $appId = $merchant['miniAppId'];
  239. $miniOpenId = $post['miniOpenId'];
  240. if (empty($miniOpenId)) {
  241. util::success(['hasNoOpenId' => 1], '登录失败');
  242. }
  243. $cacheKey = 'MALL_MINI_SESSION_KEY_' . $miniOpenId;
  244. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  245. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  246. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  247. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  248. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  249. if ($errCode != 0) {
  250. // 记录详细日志以便排查。常见错误:-41003(AES解密失败/sessionKey过期)
  251. Yii::info("小程序获取用户信息解密失败:errCode={$errCode}, miniOpenId={$miniOpenId}, appId={$appId}");
  252. util::fail("获取用户信息失败(code:{$errCode})");
  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. // 记录详细日志以便排查。常见错误:-41003(AES解密失败/sessionKey过期)
  281. Yii::info("小程序手机号解密失败:errCode={$errCode}, miniOpenId={$miniOpenId}, appId={$appId}");
  282. noticeUtil::push("小程序手机号解密失败:errCode={$errCode}, miniOpenId={$miniOpenId}, appId={$appId}", '15280215347');
  283. util::fail("解密失败(code:{$errCode}),请尝试重新登录或重试");
  284. }
  285. $arr = Json::decode($result);
  286. $mobile = $arr['purePhoneNumber'] ?? '';
  287. if (empty($mobile)) {
  288. util::fail('没有取到手机号,请重试');
  289. }
  290. util::success(['mobile' => $mobile]);
  291. }
  292. //小程序获取手机号新接口 ssh 2025.07.28
  293. //接口文档:https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/user-info/phone-number/getPhoneNumber.html
  294. public function actionGetMiniMobile()
  295. {
  296. $post = Yii::$app->request->post();
  297. $code = $post['code'] ?? '';
  298. if (empty($code)) {
  299. util::fail('参数错误:缺少code');
  300. }
  301. // 使用 miniUtil 封装好的方法获取手机号 (ptStyle=3 表示商城端)
  302. $merchant = WxOpenClass::getMallWxMiniBase();
  303. $res = \common\components\miniUtil::getPhoneNumber($merchant, $code, 3);
  304. if (isset($res['errcode']) && $res['errcode'] == 0) {
  305. $mobile = $res['phone_info']['purePhoneNumber'] ?? '';
  306. if (empty($mobile)) {
  307. util::fail('获取手机号内容为空');
  308. }
  309. util::success(['mobile' => $mobile]);
  310. } else {
  311. $errMsg = $res['errmsg'] ?? '获取手机号失败';
  312. $errCode = $res['errcode'] ?? -1;
  313. Yii::info("小程序新接口获取手机号失败:code={$errCode}, msg={$errMsg}");
  314. noticeUtil::push("小程序新接口获取手机号失败:code={$errCode}, msg={$errMsg}", '15280215347');
  315. util::fail("获取手机号失败:{$errMsg}({$errCode})");
  316. }
  317. }
  318. //密码修改
  319. public function actionPassword()
  320. {
  321. $post = Yii::$app->request->post();
  322. $old = $post['old'] ?? '';
  323. $new = $post['new'] ?? '';
  324. $confirm = $post['confirm'] ?? '';
  325. if (empty($new)) {
  326. util::fail('请输入新密码');
  327. }
  328. if ($new != $confirm) {
  329. util::fail('二次密码不一致');
  330. }
  331. $userId = $this->userId;
  332. $user = UserService::getUserInfo($userId);
  333. if (isset($user['hasPayPwd']) && $user['hasPayPwd'] == 1) {
  334. if (empty($old)) {
  335. util::fail('请填写旧密码');
  336. }
  337. if (!password_verify($old, $user['payPassword'])) {
  338. util::fail('旧密码错误');
  339. }
  340. }
  341. UserService::updateById($userId, ['payPassword' => password_hash($new, PASSWORD_BCRYPT), 'hasPayPwd' => 1]);
  342. util::complete('修改成功');
  343. }
  344. //申请会员 ssh 2019.12.18
  345. public function actionApplyMember()
  346. {
  347. $post = Yii::$app->request->post();
  348. $mobile = $post['mobile'] ?? 0;
  349. if (!stringUtil::isMobile($mobile)) {
  350. util::fail('请输入手机号');
  351. }
  352. $userId = $this->userId;
  353. $asset = UserAssetService::getByUserId($userId);
  354. if (isset($asset['member']) && $asset['member'] != -1) {
  355. util::fail('已经是会员了');
  356. }
  357. $code = $post['code'] ?? '';
  358. $cacheCode = SmsService::getApplyMemberCode($userId, $mobile);
  359. if (empty($cacheCode) || $cacheCode != $code) {
  360. util::fail('验证码错误');
  361. }
  362. $update = [];
  363. if (!empty($post['realName'])) {
  364. $update['realName'] = $post['realName'];
  365. }
  366. if (!empty($post['birthday'])) {
  367. $update['birthday'] = $post['birthday'];
  368. }
  369. $user = UserService::getByCondition(['sjId' => $this->sjId, 'mobile' => $mobile]);
  370. if ($userId == $user['id']) {
  371. util::fail('手机号已验证');
  372. }
  373. if (!empty($user)) {
  374. util::fail('手机号已经注册过了');
  375. }
  376. $update['mobile'] = $mobile;
  377. $userInfo = $this->user->attributes ?? [];
  378. UserService::becomeMember($userInfo, $this->sj->attributes, $update);
  379. util::complete('注册成功');
  380. }
  381. //登陆客户的信息 ssh 2020.3.12
  382. public function actionLoginDetail()
  383. {
  384. $user = $this->user->attributes ?? [];
  385. util::success($user);
  386. }
  387. //成长值列表
  388. public function actionGrowthList()
  389. {
  390. $where = [];
  391. $where['shopId'] = $this->shopId;
  392. $where['userId'] = $this->userId;
  393. $list = UserGrowthClass::getGrowthList($where);
  394. util::success($list);
  395. }
  396. //积分列表
  397. public function actionIntegralList()
  398. {
  399. $where = [];
  400. $where['shopId'] = $this->shopId;
  401. $where['userId'] = $this->userId;
  402. $list = UserIntegralClass::getIntegralList($where);
  403. util::success($list);
  404. }
  405. }