UserController.php 17 KB

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