UserController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. $userId = $user->id ?? 0;
  188. if ($userId > 0) {
  189. $mallUpgrading = getenv('MALL_UPGRADING') == false ? 0 : getenv('MALL_UPGRADING');
  190. if ($mallUpgrading == 1) {
  191. $allowShopAdminIdsStr = getenv('MALL_UPGRADE_ALLOW_USER_IDS') ?: '';
  192. $allowShopAdminIds = !empty($allowShopAdminIdsStr) ? explode(',', $allowShopAdminIdsStr) : [];
  193. if (!in_array($userId, $allowShopAdminIds)) {
  194. util::fail('系统升级中,稍后再试,编号:' . $userId);
  195. }
  196. }
  197. }
  198. $token = !empty($userId) ? jwt::getNewToken($userId) : '';
  199. $fullUser = UserClass::getById($userId, true);
  200. // 将 $fullUser 转换为数组,然后添加 hasMap 属性(字典中的 hasMap)
  201. $fullUser = $fullUser->toArray();
  202. $fullUser['hasMap'] = dict::getDict('hasMap');
  203. util::success(['user' => $fullUser, 'token' => $token]);
  204. }
  205. //最近店铺 ssh 2021.2.27
  206. public function actionRecentShop()
  207. {
  208. $list = [];
  209. //显示国兰的信息 0不是 1是
  210. $showGlInfo = getenv('SHOW_GL_INFO');
  211. $info = $showGlInfo === false ? 1 : $showGlInfo;
  212. util::success(['list' => $list, 'showGlInfo' => $info]);
  213. }
  214. //获取登陆客户的资产 ssh 2019.11.22
  215. public function actionAsset()
  216. {
  217. $asset = UserAssetService::getByUserId($this->userId);
  218. util::success($asset);
  219. }
  220. //获取登陆客户的优惠情况 ssh 2019.12.3
  221. public function actionDiscount()
  222. {
  223. $discount = UserAssetService::getDiscount($this->userId);
  224. util::success($discount);
  225. }
  226. //小程序用户完整信息 ssh 2019.12.12
  227. public function actionMiniFullInfo()
  228. {
  229. $post = Yii::$app->request->post();
  230. $iv = $post['iv'] ?? '';
  231. $encryptedData = $post['encryptedData'] ?? '';
  232. $merchant = WxOpenClass::getMallWxMiniBase();
  233. $appId = $merchant['miniAppId'];
  234. $miniOpenId = $post['miniOpenId'];
  235. if (empty($miniOpenId)) {
  236. util::success(['hasNoOpenId' => 1], '登录失败');
  237. }
  238. $cacheKey = 'MALL_MINI_SESSION_KEY_' . $miniOpenId;
  239. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  240. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  241. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  242. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  243. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  244. if ($errCode != 0) {
  245. Yii::info($result . ' ' . $errCode);
  246. util::fail('获取用户信息失败');
  247. }
  248. Yii::info('小程序获取用户信息:' . $result);
  249. $originalInfo = Json::decode($result);
  250. util::success($originalInfo);
  251. }
  252. //小程序用户手机号
  253. public function actionMiniMobile()
  254. {
  255. $post = Yii::$app->request->post();
  256. $iv = $post['iv'];
  257. $encryptedData = $post['encryptedData'];
  258. $merchant = WxOpenClass::getMallWxMiniBase();
  259. $appId = $merchant['miniAppId'];
  260. $miniOpenId = $post['miniOpenId'];
  261. if (empty($miniOpenId)) {
  262. util::success(['hasNoOpenId' => 1], '登录失败');
  263. }
  264. $cacheKey = 'MALL_MINI_SESSION_KEY_' . $miniOpenId;
  265. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  266. if (empty($sessionKey)) {
  267. util::fail('系统错误,session empty');
  268. }
  269. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  270. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  271. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  272. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  273. if ($errCode != 0) {
  274. util::fail('解密失败');
  275. }
  276. $arr = Json::decode($result);
  277. $mobile = $arr['purePhoneNumber'] ?? '';
  278. if (empty($mobile)) {
  279. util::fail('没有取到手机号,请重试');
  280. }
  281. util::success(['mobile' => $mobile]);
  282. }
  283. //密码修改
  284. public function actionPassword()
  285. {
  286. $post = Yii::$app->request->post();
  287. $old = $post['old'] ?? '';
  288. $new = $post['new'] ?? '';
  289. $confirm = $post['confirm'] ?? '';
  290. if (empty($new)) {
  291. util::fail('请输入新密码');
  292. }
  293. if ($new != $confirm) {
  294. util::fail('二次密码不一致');
  295. }
  296. $userId = $this->userId;
  297. $user = UserService::getUserInfo($userId);
  298. if (isset($user['hasPayPwd']) && $user['hasPayPwd'] == 1) {
  299. if (empty($old)) {
  300. util::fail('请填写旧密码');
  301. }
  302. if (!password_verify($old, $user['payPassword'])) {
  303. util::fail('旧密码错误');
  304. }
  305. }
  306. UserService::updateById($userId, ['payPassword' => password_hash($new, PASSWORD_BCRYPT), 'hasPayPwd' => 1]);
  307. util::complete('修改成功');
  308. }
  309. //申请会员 ssh 2019.12.18
  310. public function actionApplyMember()
  311. {
  312. $post = Yii::$app->request->post();
  313. $mobile = $post['mobile'] ?? 0;
  314. if (!stringUtil::isMobile($mobile)) {
  315. util::fail('请输入手机号');
  316. }
  317. $userId = $this->userId;
  318. $asset = UserAssetService::getByUserId($userId);
  319. if (isset($asset['member']) && $asset['member'] != -1) {
  320. util::fail('已经是会员了');
  321. }
  322. $code = $post['code'] ?? '';
  323. $cacheCode = SmsService::getApplyMemberCode($userId, $mobile);
  324. if (empty($cacheCode) || $cacheCode != $code) {
  325. util::fail('验证码错误');
  326. }
  327. $update = [];
  328. if (!empty($post['realName'])) {
  329. $update['realName'] = $post['realName'];
  330. }
  331. if (!empty($post['birthday'])) {
  332. $update['birthday'] = $post['birthday'];
  333. }
  334. $user = UserService::getByCondition(['sjId' => $this->sjId, 'mobile' => $mobile]);
  335. if ($userId == $user['id']) {
  336. util::fail('手机号已验证');
  337. }
  338. if (!empty($user)) {
  339. util::fail('手机号已经注册过了');
  340. }
  341. $update['mobile'] = $mobile;
  342. $userInfo = $this->user->attributes ?? [];
  343. UserService::becomeMember($userInfo, $this->sj->attributes, $update);
  344. util::complete('注册成功');
  345. }
  346. //登陆客户的信息 ssh 2020.3.12
  347. public function actionLoginDetail()
  348. {
  349. $user = $this->user->attributes ?? [];
  350. util::success($user);
  351. }
  352. //成长值列表
  353. public function actionGrowthList()
  354. {
  355. $where = [];
  356. $where['shopId'] = $this->shopId;
  357. $where['userId'] = $this->userId;
  358. $list = UserGrowthClass::getGrowthList($where);
  359. util::success($list);
  360. }
  361. //积分列表
  362. public function actionIntegralList()
  363. {
  364. $where = [];
  365. $where['shopId'] = $this->shopId;
  366. $where['userId'] = $this->userId;
  367. $list = UserIntegralClass::getIntegralList($where);
  368. util::success($list);
  369. }
  370. }