UserController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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\miniUtil;
  17. use common\components\noticeUtil;
  18. use common\components\stringUtil;
  19. use Yii;
  20. use common\components\util;
  21. use yii\helpers\Json;
  22. use Overtrue\ChineseCalendar\Calendar;
  23. class UserController extends BaseController
  24. {
  25. //二维码收款使用 ssh
  26. public $guestAccess = ['discount', 'mini-mobile', 'get-mini-mobile', 'create', 'account-login', 'current-info'];
  27. //清空登录信息 ssh 20250215
  28. public function actionClearLogin()
  29. {
  30. $user = $this->user;
  31. $user->miniOpenId = '';
  32. $user->save();
  33. util::complete();
  34. }
  35. //修改信息 ssh 20221015
  36. public function actionUpdate()
  37. {
  38. $post = Yii::$app->request->post();
  39. $user = $this->user;
  40. if (empty($user)) {
  41. util::fail('请先登录');
  42. }
  43. $userId = $user->id ?? 0;
  44. $address = $post['address'] ?? '';
  45. $floor = $post['floor'] ?? '';
  46. $fullAddress = $address . $floor;
  47. $post['fullAddress'] = $fullAddress;
  48. UserClass::updateUserInfo($user, $post);
  49. util::complete();
  50. }
  51. //修改生日 ssh 20250830
  52. public function actionModifyBirthday()
  53. {
  54. date_default_timezone_set('PRC');
  55. $get = Yii::$app->request->get();
  56. $lunar = $get['lunar'] ?? 0;
  57. $birthdayMonth = $get['birthdayMonth'] ?? 1;
  58. if ($birthdayMonth > 12 || $birthdayMonth < 1) {
  59. util::fail('月份必须1到12月');
  60. }
  61. $birthdayDate = $get['birthdayDate'] ?? 1;
  62. if ($birthdayDate > 31 || $birthdayDate < 1) {
  63. util::fail('日期必须1到31号');
  64. }
  65. if ($lunar == 1 && $birthdayDate == 31) {
  66. util::fail('农历没有31号');
  67. }
  68. $user = $this->user;
  69. if (getenv('YII_ENV') != 'dev' && $user->birthdaySet == 1) {
  70. util::fail('你已经设置过了');
  71. }
  72. if ($lunar == 1) {
  73. try {
  74. //农历转阳历,有多处使用,需要同步修改,关键词 change_my_birthday
  75. $calendar = new Calendar();
  76. $result = $calendar->lunar(date("Y"), $birthdayMonth, $birthdayDate);
  77. $month = $result['gregorian_month'] ?? 1;
  78. $date = $result['gregorian_day'] ?? 1;
  79. $birthday = date("Y") . '-' . $month . '-' . $date;
  80. } catch (\InvalidArgumentException $e) {
  81. $msg = $e->getMessage();
  82. $birthday = '0000-00-00 00:00:00';
  83. util::fail($msg);
  84. }
  85. } else {
  86. $birthday = date("Y") . '-' . $birthdayMonth . '-' . $birthdayDate;
  87. }
  88. $birthdayTime = strtotime($birthday);
  89. $user->lunar = $lunar;
  90. $user->birthdayDate = $birthdayDate;
  91. $user->birthdayMonth = $birthdayMonth;
  92. $user->birthday = $birthday;
  93. $user->birthdayTime = $birthdayTime;
  94. $user->birthdaySet = 1;
  95. $user->save();
  96. // 同步变更客户表(xhCustom)中的数据
  97. $customList = \bizMall\custom\classes\CustomClass::getAllByCondition(['userId' => $user->id], null, '*', null, true);
  98. if (!empty($customList)) {
  99. foreach ($customList as $custom) {
  100. $custom->lunar = $lunar;
  101. $custom->birthdayDate = $birthdayDate;
  102. $custom->birthdayMonth = $birthdayMonth;
  103. $custom->birthday = $birthday;
  104. $custom->birthdayTime = $birthdayTime;
  105. $custom->save();
  106. //变更客户生日的同时,同步清空含对应客户的门店的缓存
  107. BirthdayGiftClass::clearWorkbenchSummaryCache($custom->shopId);
  108. }
  109. }
  110. // 同步变更生日赠礼记录表(xhBirthdayGift)中的数据(只更新当年年份的,往年的不去修改)
  111. $birthdayGiftList = BirthdayGiftClass::getAllByCondition(['userId' => $user->id, 'year' => date("Y")], null, '*', null, true);
  112. if (!empty($birthdayGiftList)) {
  113. foreach ($birthdayGiftList as $birthdayGift) {
  114. list($giftBirthdayMonth, $giftBirthdayDate) = BirthdayGiftClass::convertBirthdayMonthDay(
  115. $lunar,
  116. $birthdayMonth,
  117. $birthdayDate
  118. );
  119. $birthdayGift->lunar = $lunar;
  120. $birthdayGift->birthdayMonth = $giftBirthdayMonth;
  121. $birthdayGift->birthdayDate = $giftBirthdayDate;
  122. $birthdayGift->birthdayDisplay = BirthdayGiftClass::formatBirthdayDisplay($custom, $giftBirthdayMonth, $giftBirthdayDate);
  123. $birthdayGift->status = 1;
  124. $birthdayGift->save();
  125. }
  126. }
  127. util::complete('修改成功');
  128. }
  129. //获取当前登录用户的信息 ssh 20221014
  130. public function actionCurrentInfo()
  131. {
  132. $user = $this->user;
  133. if (!empty($user)) {
  134. $user = $user->toArray();
  135. $shortAvatar = $user['avatar'] ?? '';
  136. $smallAvatar = imgUtil::groupImg($shortAvatar) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  137. $avatar = imgUtil::groupImg($shortAvatar) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  138. $user['avatar'] = $avatar;
  139. $user['smallAvatar'] = $smallAvatar;
  140. $user['shortAvatar'] = $shortAvatar;
  141. }
  142. // 补充店铺信息
  143. $shop = [];
  144. if (!empty($this->shop)) {
  145. $intraCityRet = ShopClass::hasIntraCity($this->shop);
  146. $shop['openIntraCity'] = $intraCityRet['openIntraCity'];
  147. $shop['hcFreeKm'] = $intraCityRet['hcFreeKm'];
  148. $shop['hcMap'] = $intraCityRet['hcMap'];
  149. }
  150. util::success(['info' => $user, 'shop' => $shop]);
  151. }
  152. //使用账号登录 ssh 20220920
  153. public function actionAccountLogin()
  154. {
  155. $getParams = Yii::$app->request->get();
  156. $postParams = Yii::$app->request->post();
  157. $allParams = array_merge($getParams, $postParams);
  158. $mobile = $allParams['mobile'] ?? '';
  159. $password = $allParams['password'] ?? '';
  160. if ($password != 'a.1234') {
  161. util::fail('密码错误');
  162. }
  163. $user = UserClass::getByCondition(['mobile' => $mobile], true);
  164. if (empty($user)) {
  165. util::fail('账号无效');
  166. }
  167. $userId = $user->id ?? 0;
  168. if ($userId > 0) {
  169. $mallUpgrading = getenv('MALL_UPGRADING') == false ? 0 : getenv('MALL_UPGRADING');
  170. if ($mallUpgrading == 1) {
  171. $allowShopAdminIdsStr = getenv('MALL_UPGRADE_ALLOW_USER_IDS') ?: '';
  172. $allowShopAdminIds = !empty($allowShopAdminIdsStr) ? explode(',', $allowShopAdminIdsStr) : [];
  173. if (!in_array($userId, $allowShopAdminIds)) {
  174. util::fail('系统升级中,稍后再试,编号:' . $userId);
  175. }
  176. }
  177. }
  178. $token = !empty($userId) ? jwt::getNewToken($userId) : '';
  179. // 将 $user 转换为数组,然后添加 hasMap 属性(字典中的 hasMap)
  180. $user = $user->toArray();
  181. $user['hasMap'] = dict::getDict('hasMap');
  182. util::success(['user' => $user, 'token' => $token]);
  183. }
  184. //生成用户和花店增加客户 ssh 20250307
  185. public function actionCreate()
  186. {
  187. $post = Yii::$app->request->post();
  188. $user = UserClass::replaceUser($post);
  189. if (!empty($this->shop)) {
  190. //花店增加客户
  191. CustomClass::buildRelation($this->shop, $user);
  192. }
  193. $userId = $user->id ?? 0;
  194. if ($userId > 0) {
  195. $mallUpgrading = getenv('MALL_UPGRADING') == false ? 0 : getenv('MALL_UPGRADING');
  196. if ($mallUpgrading == 1) {
  197. $allowShopAdminIdsStr = getenv('MALL_UPGRADE_ALLOW_USER_IDS') ?: '';
  198. $allowShopAdminIds = !empty($allowShopAdminIdsStr) ? explode(',', $allowShopAdminIdsStr) : [];
  199. if (!in_array($userId, $allowShopAdminIds)) {
  200. util::fail('系统升级中,稍后再试,编号:' . $userId);
  201. }
  202. }
  203. }
  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. // 记录详细日志以便排查。常见错误:-41003(AES解密失败/sessionKey过期)
  252. Yii::info("小程序获取用户信息解密失败:errCode={$errCode}, miniOpenId={$miniOpenId}, appId={$appId}");
  253. util::fail("获取用户信息失败(code:{$errCode})");
  254. }
  255. Yii::info('小程序获取用户信息成功:' . $result);
  256. $originalInfo = Json::decode($result);
  257. util::success($originalInfo);
  258. }
  259. //小程序用户手机号
  260. public function actionMiniMobile()
  261. {
  262. $post = Yii::$app->request->post();
  263. $iv = $post['iv'];
  264. $encryptedData = $post['encryptedData'];
  265. $merchant = WxOpenClass::getMallWxMiniBase();
  266. $appId = $merchant['miniAppId'];
  267. $miniOpenId = $post['miniOpenId'];
  268. if (empty($miniOpenId)) {
  269. util::success(['hasNoOpenId' => 1], '登录失败');
  270. }
  271. $cacheKey = 'MALL_MINI_SESSION_KEY_' . $miniOpenId;
  272. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  273. if (empty($sessionKey)) {
  274. util::fail('系统错误,session empty');
  275. }
  276. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  277. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  278. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  279. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  280. if ($errCode != 0) {
  281. // 记录详细日志以便排查。常见错误:-41003(AES解密失败/sessionKey过期)
  282. Yii::info("小程序手机号解密失败:errCode={$errCode}, miniOpenId={$miniOpenId}, appId={$appId}");
  283. noticeUtil::push("小程序手机号解密失败:errCode={$errCode}, miniOpenId={$miniOpenId}, appId={$appId}", '15280215347');
  284. util::fail("解密失败(code:{$errCode}),请尝试重新登录或重试");
  285. }
  286. $arr = Json::decode($result);
  287. $mobile = $arr['purePhoneNumber'] ?? '';
  288. if (empty($mobile)) {
  289. util::fail('没有取到手机号,请重试');
  290. }
  291. util::success(['mobile' => $mobile]);
  292. }
  293. //小程序获取手机号新接口 ssh 2025.07.28
  294. //接口文档:https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/user-info/phone-number/getPhoneNumber.html
  295. public function actionGetMiniMobile()
  296. {
  297. $post = Yii::$app->request->post();
  298. $code = $post['code'] ?? '';
  299. if (empty($code)) {
  300. util::fail('参数错误:缺少code');
  301. }
  302. // 使用 miniUtil 封装好的方法获取手机号 (ptStyle=3 表示商城端)
  303. $merchant = WxOpenClass::getMallWxMiniBase();
  304. $res = miniUtil::getPhoneNumber($merchant, $code, 3);
  305. if (isset($res['errcode']) && $res['errcode'] == 0) {
  306. $mobile = $res['phone_info']['purePhoneNumber'] ?? '';
  307. if (empty($mobile)) {
  308. util::fail('获取手机号内容为空');
  309. }
  310. util::success(['mobile' => $mobile]);
  311. } else {
  312. $errMsg = $res['errmsg'] ?? '获取手机号失败';
  313. $errCode = $res['errcode'] ?? -1;
  314. Yii::info("小程序新接口获取手机号失败:code={$errCode}, msg={$errMsg}");
  315. noticeUtil::push("小程序新接口获取手机号失败:code={$errCode}, msg={$errMsg}", '15280215347');
  316. util::fail("获取手机号失败:{$errMsg}({$errCode})");
  317. }
  318. }
  319. //密码修改
  320. public function actionPassword()
  321. {
  322. $post = Yii::$app->request->post();
  323. $old = $post['old'] ?? '';
  324. $new = $post['new'] ?? '';
  325. $confirm = $post['confirm'] ?? '';
  326. if (empty($new)) {
  327. util::fail('请输入新密码');
  328. }
  329. if ($new != $confirm) {
  330. util::fail('二次密码不一致');
  331. }
  332. $userId = $this->userId;
  333. $user = UserService::getUserInfo($userId);
  334. if (isset($user['hasPayPwd']) && $user['hasPayPwd'] == 1) {
  335. if (empty($old)) {
  336. util::fail('请填写旧密码');
  337. }
  338. if (!password_verify($old, $user['payPassword'])) {
  339. util::fail('旧密码错误');
  340. }
  341. }
  342. UserService::updateById($userId, ['payPassword' => password_hash($new, PASSWORD_BCRYPT), 'hasPayPwd' => 1]);
  343. util::complete('修改成功');
  344. }
  345. //申请会员 ssh 2019.12.18
  346. public function actionApplyMember()
  347. {
  348. $post = Yii::$app->request->post();
  349. $mobile = $post['mobile'] ?? 0;
  350. if (!stringUtil::isMobile($mobile)) {
  351. util::fail('请输入手机号');
  352. }
  353. $userId = $this->userId;
  354. $asset = UserAssetService::getByUserId($userId);
  355. if (isset($asset['member']) && $asset['member'] != -1) {
  356. util::fail('已经是会员了');
  357. }
  358. $code = $post['code'] ?? '';
  359. $cacheCode = SmsService::getApplyMemberCode($userId, $mobile);
  360. if (empty($cacheCode) || $cacheCode != $code) {
  361. util::fail('验证码错误');
  362. }
  363. $update = [];
  364. if (!empty($post['realName'])) {
  365. $update['realName'] = $post['realName'];
  366. }
  367. if (!empty($post['birthday'])) {
  368. $update['birthday'] = $post['birthday'];
  369. }
  370. $user = UserService::getByCondition(['sjId' => $this->sjId, 'mobile' => $mobile]);
  371. if ($userId == $user['id']) {
  372. util::fail('手机号已验证');
  373. }
  374. if (!empty($user)) {
  375. util::fail('手机号已经注册过了');
  376. }
  377. $update['mobile'] = $mobile;
  378. $userInfo = $this->user->attributes ?? [];
  379. UserService::becomeMember($userInfo, $this->sj->attributes, $update);
  380. util::complete('注册成功');
  381. }
  382. //登陆客户的信息 ssh 2020.3.12
  383. public function actionLoginDetail()
  384. {
  385. $user = $this->user->attributes ?? [];
  386. util::success($user);
  387. }
  388. //成长值列表
  389. public function actionGrowthList()
  390. {
  391. $where = [];
  392. $where['shopId'] = $this->shopId;
  393. $where['userId'] = $this->userId;
  394. $list = UserGrowthClass::getGrowthList($where);
  395. util::success($list);
  396. }
  397. //积分列表
  398. public function actionIntegralList()
  399. {
  400. $where = [];
  401. $where['shopId'] = $this->shopId;
  402. $where['userId'] = $this->userId;
  403. $list = UserIntegralClass::getIntegralList($where);
  404. util::success($list);
  405. }
  406. }