UserController.php 15 KB

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