UserController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. <?php
  2. namespace mall\controllers;
  3. use biz\shop\classes\ShopExtClass;
  4. use bizHd\birthday\classes\BirthdayGiftClass;
  5. use bizHd\custom\classes\CustomClass;
  6. use bizHd\wx\classes\WxOpenClass;
  7. use bizMall\message\services\SmsService;
  8. use bizMall\user\classes\UserClass;
  9. use bizMall\user\classes\UserGrowthClass;
  10. use bizMall\user\classes\UserIntegralClass;
  11. use bizMall\user\services\UserAssetService;
  12. use bizMall\user\services\UserService;
  13. use biz\shop\classes\ShopClass;
  14. use common\components\dict;
  15. use common\components\imgUtil;
  16. use common\components\jwt;
  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', '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. }
  108. // 同步变更生日赠礼记录表(xhBirthdayGift)中的数据(只更新当年年份的,往年的不去修改)
  109. $birthdayGiftList = BirthdayGiftClass::getAllByCondition(['userId' => $user->id, 'year' => date("Y")], null, '*', null, true);
  110. if (!empty($birthdayGiftList)) {
  111. foreach ($birthdayGiftList as $birthdayGift) {
  112. list($giftBirthdayMonth, $giftBirthdayDate) = BirthdayGiftClass::convertBirthdayMonthDay(
  113. $lunar,
  114. $birthdayMonth,
  115. $birthdayDate
  116. );
  117. $birthdayGift->lunar = $lunar;
  118. $birthdayGift->birthdayMonth = $giftBirthdayMonth;
  119. $birthdayGift->birthdayDate = $giftBirthdayDate;
  120. $birthdayGift->birthdayDisplay = BirthdayGiftClass::formatBirthdayDisplay($custom, $giftBirthdayMonth, $giftBirthdayDate);
  121. $birthdayGift->status = 1;
  122. $birthdayGift->save();
  123. }
  124. }
  125. util::complete('修改成功');
  126. }
  127. //获取当前登录用户的信息 ssh 20221014
  128. public function actionCurrentInfo()
  129. {
  130. $user = $this->user;
  131. if (!empty($user)) {
  132. $birthday = $user->birthday ?? '';
  133. //如果当年的生日时间没有更新,则需要给更新
  134. $year = date("Y", strtotime($birthday));
  135. $thisYear = date("Y");
  136. if ($year != $thisYear) {
  137. $lunar = $user->lunar ?? 0;
  138. $userId = $this->userId;
  139. $birthdayMonth = $user->birthdayMonth ?? 0;
  140. $birthdayDate = $user->birthdayDate ?? 0;
  141. if ($lunar == 1 && $birthdayMonth > 0 && $birthdayDate > 0) {
  142. try {
  143. //农历转阳历,有多处使用,需要同步修改,关键词change_my_birthday
  144. $calendar = new Calendar();
  145. $result = $calendar->lunar(date("Y"), $birthdayMonth, $birthdayDate);
  146. $month = $result['gregorian_month'] ?? 1;
  147. $date = $result['gregorian_day'] ?? 1;
  148. $birthday = date("Y") . '-' . $month . '-' . $date;
  149. $user->birthday = $birthday;
  150. $user->save();
  151. CustomClass::updateByCondition(['userId' => $userId], ['birthday' => $birthday]);
  152. } catch (\InvalidArgumentException $e) {
  153. $msg = $e->getMessage();
  154. noticeUtil::push("更新生日出错了呢,userId:" . $userId . ' ' . $msg, '15280215347');
  155. }
  156. } else {
  157. $birthday = date("Y") . '-' . $birthdayMonth . '-' . $birthdayDate;
  158. $user->birthday = $birthday;
  159. $user->save();
  160. CustomClass::updateByCondition(['userId' => $userId], ['birthday' => $birthday]);
  161. }
  162. }
  163. // 将 $user 转换为数组,然后添加 hasMap 属性(字典中的 hasMap)
  164. $user = $user->toArray();
  165. $shortAvatar = $user['avatar'] ?? '';
  166. $smallAvatar = imgUtil::groupImg($shortAvatar) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  167. $avatar = imgUtil::groupImg($shortAvatar) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  168. $user['avatar'] = $avatar;
  169. $user['smallAvatar'] = $smallAvatar;
  170. $user['shortAvatar'] = $shortAvatar;
  171. }
  172. // 补充店铺信息
  173. $shop = [];
  174. if (!empty($this->shop)) {
  175. $intraCityRet = ShopClass::hasIntraCity($this->shop);
  176. $shop['openIntraCity'] = $intraCityRet['openIntraCity'];
  177. $shop['hcFreeKm'] = $intraCityRet['hcFreeKm'];
  178. $shop['hcMap'] = $intraCityRet['hcMap'];
  179. }
  180. util::success(['info' => $user, 'shop' => $shop]);
  181. }
  182. //使用账号登录 ssh 20220920
  183. public function actionAccountLogin()
  184. {
  185. $getParams = Yii::$app->request->get();
  186. $postParams = Yii::$app->request->post();
  187. $allParams = array_merge($getParams, $postParams);
  188. $mobile = $allParams['mobile'] ?? '';
  189. $password = $allParams['password'] ?? '';
  190. if ($password != 'a.1234') {
  191. util::fail('密码错误');
  192. }
  193. $user = UserClass::getByCondition(['mobile' => $mobile], true);
  194. if (empty($user)) {
  195. util::fail('账号无效');
  196. }
  197. $userId = $user->id ?? 0;
  198. if ($userId > 0) {
  199. $mallUpgrading = getenv('MALL_UPGRADING') == false ? 0 : getenv('MALL_UPGRADING');
  200. if ($mallUpgrading == 1) {
  201. $allowShopAdminIdsStr = getenv('MALL_UPGRADE_ALLOW_USER_IDS') ?: '';
  202. $allowShopAdminIds = !empty($allowShopAdminIdsStr) ? explode(',', $allowShopAdminIdsStr) : [];
  203. if (!in_array($userId, $allowShopAdminIds)) {
  204. util::fail('系统升级中,稍后再试,编号:' . $userId);
  205. }
  206. }
  207. }
  208. $token = !empty($userId) ? jwt::getNewToken($userId) : '';
  209. // 将 $user 转换为数组,然后添加 hasMap 属性(字典中的 hasMap)
  210. $user = $user->toArray();
  211. $user['hasMap'] = dict::getDict('hasMap');
  212. util::success(['user' => $user, 'token' => $token]);
  213. }
  214. //生成用户和花店增加客户 ssh 20250307
  215. public function actionCreate()
  216. {
  217. $post = Yii::$app->request->post();
  218. $user = UserClass::replaceUser($post);
  219. if (!empty($this->shop)) {
  220. //花店增加客户
  221. CustomClass::buildRelation($this->shop, $user);
  222. }
  223. $userId = $user->id ?? 0;
  224. if ($userId > 0) {
  225. $mallUpgrading = getenv('MALL_UPGRADING') == false ? 0 : getenv('MALL_UPGRADING');
  226. if ($mallUpgrading == 1) {
  227. $allowShopAdminIdsStr = getenv('MALL_UPGRADE_ALLOW_USER_IDS') ?: '';
  228. $allowShopAdminIds = !empty($allowShopAdminIdsStr) ? explode(',', $allowShopAdminIdsStr) : [];
  229. if (!in_array($userId, $allowShopAdminIds)) {
  230. util::fail('系统升级中,稍后再试,编号:' . $userId);
  231. }
  232. }
  233. }
  234. $token = !empty($userId) ? jwt::getNewToken($userId) : '';
  235. $fullUser = UserClass::getById($userId, true);
  236. // 将 $fullUser 转换为数组,然后添加 hasMap 属性(字典中的 hasMap)
  237. $fullUser = $fullUser->toArray();
  238. $fullUser['hasMap'] = dict::getDict('hasMap');
  239. util::success(['user' => $fullUser, 'token' => $token]);
  240. }
  241. //最近店铺 ssh 2021.2.27
  242. public function actionRecentShop()
  243. {
  244. $list = [];
  245. //显示国兰的信息 0不是 1是
  246. $showGlInfo = getenv('SHOW_GL_INFO');
  247. $info = $showGlInfo === false ? 1 : $showGlInfo;
  248. util::success(['list' => $list, 'showGlInfo' => $info]);
  249. }
  250. //获取登陆客户的资产 ssh 2019.11.22
  251. public function actionAsset()
  252. {
  253. $asset = UserAssetService::getByUserId($this->userId);
  254. util::success($asset);
  255. }
  256. //获取登陆客户的优惠情况 ssh 2019.12.3
  257. public function actionDiscount()
  258. {
  259. $discount = UserAssetService::getDiscount($this->userId);
  260. util::success($discount);
  261. }
  262. //小程序用户完整信息 ssh 2019.12.12
  263. public function actionMiniFullInfo()
  264. {
  265. $post = Yii::$app->request->post();
  266. $iv = $post['iv'] ?? '';
  267. $encryptedData = $post['encryptedData'] ?? '';
  268. $merchant = WxOpenClass::getMallWxMiniBase();
  269. $appId = $merchant['miniAppId'];
  270. $miniOpenId = $post['miniOpenId'];
  271. if (empty($miniOpenId)) {
  272. util::success(['hasNoOpenId' => 1], '登录失败');
  273. }
  274. $cacheKey = 'MALL_MINI_SESSION_KEY_' . $miniOpenId;
  275. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  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. Yii::info($result . ' ' . $errCode);
  282. util::fail('获取用户信息失败');
  283. }
  284. Yii::info('小程序获取用户信息:' . $result);
  285. $originalInfo = Json::decode($result);
  286. util::success($originalInfo);
  287. }
  288. //小程序用户手机号
  289. public function actionMiniMobile()
  290. {
  291. $post = Yii::$app->request->post();
  292. $iv = $post['iv'];
  293. $encryptedData = $post['encryptedData'];
  294. $merchant = WxOpenClass::getMallWxMiniBase();
  295. $appId = $merchant['miniAppId'];
  296. $miniOpenId = $post['miniOpenId'];
  297. if (empty($miniOpenId)) {
  298. util::success(['hasNoOpenId' => 1], '登录失败');
  299. }
  300. $cacheKey = 'MALL_MINI_SESSION_KEY_' . $miniOpenId;
  301. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  302. if (empty($sessionKey)) {
  303. util::fail('系统错误,session empty');
  304. }
  305. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  306. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  307. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  308. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  309. if ($errCode != 0) {
  310. util::fail('解密失败');
  311. }
  312. $arr = Json::decode($result);
  313. $mobile = $arr['purePhoneNumber'] ?? '';
  314. if (empty($mobile)) {
  315. util::fail('没有取到手机号,请重试');
  316. }
  317. util::success(['mobile' => $mobile]);
  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. }