UserController.php 94 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\client\controllers\v1;
  8. use app\constants\OptionSetting;
  9. use app\librarys\Picqer\Barcode\BarcodeGeneratorJPG;
  10. use app\models\Admin;
  11. use app\models\AgentGoodsBindGoods;
  12. use app\models\AgentGoodsInstallLog;
  13. use app\models\BrowseLog;
  14. use app\models\Cash;
  15. use app\models\Favorite;
  16. use app\models\Goods;
  17. use app\models\Level;
  18. use app\models\Address;
  19. use app\models\Lg;
  20. use app\models\MdStaff;
  21. use app\models\Option;
  22. use app\models\Order;
  23. use app\models\OrderDetail;
  24. use app\models\OrderRefund;
  25. use app\models\QueueAccountLog;
  26. use app\models\QueueLog;
  27. use app\models\Register;
  28. use app\models\SaasUser;
  29. use app\models\ShareHolder;
  30. use app\models\Store;
  31. use app\models\StoreShareMoney;
  32. use app\models\Supplier;
  33. use app\models\TeamGrades;
  34. use app\models\TeamGradesLevel;
  35. use app\models\User;
  36. use app\models\UserAuditLog;
  37. use app\models\UserAuditSetting;
  38. use app\models\UserCard;
  39. use app\models\UserCoupon;
  40. use app\models\UserGrowthValueLog;
  41. use app\models\VerifyCardSale;
  42. use app\models\VideoGoodsAuthor;
  43. use app\models\WechatConfig;
  44. use app\modules\admin\models\AlipayThirdForm;
  45. use app\modules\admin\models\UserCenterForm;
  46. use app\modules\client\behaviors\Auth;
  47. use app\modules\client\controllers\BaseController;
  48. use app\modules\client\models\v1\CardListForm;
  49. use app\modules\client\models\v1\LevelPayDataForm;
  50. use app\modules\client\models\v1\LgForm;
  51. use app\modules\client\models\v1\OrderMemberForm;
  52. use app\modules\client\models\v1\QrcodeForm;
  53. use app\modules\client\models\v1\SaasUserForm;
  54. use app\modules\client\models\v1\UserForm;
  55. use app\modules\client\models\v1\AddressSaveForm;
  56. use app\modules\client\models\v1\AddressSetDefaultForm;
  57. use app\modules\client\models\v1\FavoriteAddForm;
  58. use app\modules\client\models\v1\FavoriteListForm;
  59. use app\modules\client\models\v1\TopicFavoriteForm;
  60. use app\modules\client\models\v1\TopicFavoriteListForm;
  61. use app\modules\client\models\v1\OrderListForm;
  62. use app\modules\client\models\v1\ShareForm;
  63. use app\utils\Alipay\alipaySdk\aop\request\AlipaySystemOauthTokenRequest;
  64. use app\utils\LgApi;
  65. use app\utils\Notice\NoticeSend;
  66. use app\utils\QrCode;
  67. use app\utils\Sms;
  68. use app\utils\ShareQrcode;
  69. use app\utils\Wechat\Wechat;
  70. use app\utils\Wechat\WechatMini;
  71. use EasyWeChat\Kernel\Exceptions\HttpException;
  72. use Overtrue\Socialite\SocialiteManager;
  73. use yii\base\BaseObject;
  74. use yii\data\Pagination;
  75. use yii\helpers\Json;
  76. use app\models\AccountLog;
  77. use app\models\Cart;
  78. use app\modules\client\models\v1\LoginForm;
  79. use EasyWeChat\Kernel\BaseClient;
  80. use app\modules\admin\models\WastoreForm;
  81. use app\models\UserPayMonth;
  82. use app\utils\OrderUtil;
  83. /**
  84. * 用户
  85. */
  86. class UserController extends BaseController
  87. {
  88. public function behaviors()
  89. {
  90. return parent::behaviors();
  91. }
  92. public function actionSessionGet() {
  93. $form = new WastoreForm();
  94. $mini_id = input_params('mini_id');
  95. $session_key = input_params('session_key');
  96. return $this->asJson($form->wastoreSessionGet(get_store_id(), $mini_id, $session_key));
  97. }
  98. /**
  99. * 用户中心
  100. * @return \yii\web\Response
  101. */
  102. public function actionIndex()
  103. {
  104. $data = self::getDefaultIndexData();
  105. $data = [
  106. 'code' => 0,
  107. 'data' => $data,
  108. 'msg' => 'success'
  109. ];
  110. $saasUser = get_saas_user();
  111. $is_md_staff = 0;
  112. $is_md_manager = 0;
  113. $md_staff = null;
  114. $is_admin = 0;
  115. if ($saasUser) {
  116. $md_staff = MdStaff::findOne(['saas_user_id' => $saasUser['id'], 'is_delete' => 0, 'is_disable' => 0, 'store_id' => get_store_id()]);
  117. $is_admin = !empty($saasUser['id']) ? Admin::findOne(['saas_user_id' => $saasUser['id'], 'is_delete' => 0,'type'=>"admin"]) : "";
  118. }
  119. if ($md_staff) {
  120. $is_md_staff = 1;
  121. $is_md_manager = $md_staff->is_manager;
  122. }
  123. if (is_platform()) {
  124. $user_data_info = [
  125. 'access_token' => $saasUser['access_token'],
  126. 'avatar_url' => $saasUser['avatar'],
  127. 'binding' => $saasUser['mobile'],
  128. 'id' => $saasUser['id'],
  129. 'nickname' => $saasUser['name'],
  130. 'saas_money' => $saasUser['share_profit'],
  131. 'is_salesman' => $saasUser['is_salesman'],
  132. 'is_md_staff' => $is_md_staff,
  133. 'is_md_manager' => $is_md_manager,
  134. 'is_admin' => empty($is_admin) ? 0 : 1
  135. ];
  136. $data['data']['user_info'] = $user_data_info;
  137. } elseif (get_user()) {
  138. $user = User::find()->alias('u')
  139. ->leftJoin(['l' => Level::tableName()], 'u.level=l.level and l.is_delete = 0 and l.store_id='.get_store_id())
  140. ->leftJoin(['p' => User::tableName()], 'u.old_parent_id=p.id')
  141. ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=p.binding')
  142. ->where(['u.id' => get_user_id(), 'u.store_id' => get_store_id()])->select('u.*, IFNULL(l.name, "普通用户") as `level_name`, IFNULL(su.name, "总店") as `parent`')
  143. ->asArray()->one();
  144. //$admin = Admin::find()->where(['mobile' => $user['binding'], 'is_delete' => 0, 'type' => 'store'])->select('id, type_id')->one();
  145. $is_admin = 0;
  146. if (\Yii::$app->prod_is_dandianpu() && get_user()->is_admin == 0) {
  147. $store = Store::findOne(get_store_id());
  148. if ($store && $store->admin_id > 0) {
  149. $store_admin = Admin::findOne(['id' => $store->admin_id, 'is_delete' => 0]);
  150. if ($store_admin) {
  151. $admin_user = User::findOne(['binding' => $store_admin->mobile, 'store_id' => get_store_id(), 'is_delete' => 0]);
  152. if ($admin_user) {
  153. $is_admin = 1;
  154. $admin_user->is_admin = 1;
  155. $admin_user->save();
  156. }
  157. }
  158. }
  159. } else {
  160. $is_admin = (get_user()->is_admin . '');
  161. }
  162. $user_data_info = [
  163. 'access_token' => $saasUser['access_token'],
  164. 'avatar_url' => get_user()['avatar_url'],
  165. 'binding' => $user['binding'],
  166. 'blacklist' => $user['blacklist'],
  167. 'id' => $user['id'],
  168. 'is_admin' => $is_admin,
  169. 'is_clerk' => $user['is_clerk'],
  170. 'is_saas_clerk' => (int)$user['is_saas_clerk'],
  171. 'is_delivery' => $user['is_delivery'],
  172. 'is_distributor' => $user['is_distributor'],
  173. 'is_live' => 0,
  174. 'is_real' => $user['is_real'],
  175. 'level' => $user['level'],
  176. 'level_name' => $user['level_name'],
  177. 'nickname' => get_user()['nickname'],
  178. 'parent' => $user['parent'],
  179. 'integral' => $user['integral'] ? $user['integral'] : 0,
  180. 'money' => $user['money'] ? $user['money'] : "0.00",
  181. 'price' => $user['price'] ? $user['price'] : '0.00',
  182. 'gender' => $user['gender'],
  183. 'saas_money' => SaasUser::findOne(['mobile' => $user['binding']])->share_profit,
  184. 'is_salesman' => $saasUser['is_salesman'],
  185. 'is_md_staff' => $is_md_staff,
  186. 'is_md_manager' => $is_md_manager,
  187. 'user_id' => get_user_id(),
  188. 'is_cashier' => $user['is_cashier'] == 1 && $user['cashier_status'] == 1 ? 1 : 0
  189. ];
  190. if (is_h5()) {
  191. $wechat_config = WechatConfig::findOne(['store_id' => get_store_id(), 'type' => WechatConfig::TYPE_CONFIG_MP]);
  192. $one_store_wechat_official_appid = Option::get('one_store_wechat_official_appid', 0, 'saas', '')['value'];
  193. if (!empty($wechat_config) || !empty($one_store_wechat_official_appid)) {
  194. // h5端,如果openid为空,返回授权链接
  195. // Wechat::init(get_store_id(), Wechat::WECHAT_KIND_OFFICIAL, WechatConfig::TYPE_CONFIG_MP);
  196. // $baseUrl = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl;
  197. // $store_id = get_store_id();
  198. // $redirect_url = $baseUrl . '/index.php?r=client/v1/user/update-user-info&store_id='. $store_id .'&access_token=' . $saasUser['access_token'];
  199. // $user_data_info['oauth_redirect_url'] = Wechat::$wechat_official->oauth->scopes(['snsapi_userinfo'])->redirect($redirect_url);
  200. $user_data_info['oauth_redirect_url'] = LoginForm::getAuthLink();
  201. }
  202. }
  203. if (\Yii::$app->prod_is_dandianpu()) {
  204. $user_data_info['is_show_toggle_store'] = 0; // 大于0表示显示切换,并且该值是store_id
  205. $user_data_info['has_store'] = 0; // 当前用户是否有商城
  206. $admin = Admin::findOne(['saas_user_id' => $saasUser['id'], 'is_delete' => 0, 'type' => 'store']);
  207. if ($admin) {
  208. $store = Store::findOne(['id' => $admin->type_id, 'is_delete' => 0]);
  209. if ($store) {
  210. $user_data_info['has_store'] = $store->id;
  211. }
  212. $self_mini = intval(Option::get('self_mini', $store->id, 'store', 0)['value']);
  213. if ($store && $store->id != get_store_id() && !$self_mini) {
  214. $user_data_info['is_show_toggle_store'] = $store->id;
  215. }
  216. }
  217. }
  218. $share_holder = ShareHolder::findOne(['user_id' => get_user_id(), 'is_delete' => 0, 'store_id' => get_store_id(), 'status' => 1]);
  219. $user_data_info['is_holder'] = $share_holder ? 1 : 0;
  220. //抖品作者
  221. $videoAuthor = VideoGoodsAuthor::findOne(['user_id' => get_user_id(), 'is_delete' => 0, 'store_id' => get_store_id(), 'status' => 1]);
  222. $user_data_info['is_video_author'] = $videoAuthor ? 1 : 0;
  223. $data['data']['user_info'] = $user_data_info;
  224. }
  225. /**
  226. * 获取用户优惠券数量
  227. */
  228. $coupon_num = UserCoupon::find()->where([
  229. 'user_id' => get_user_id(),
  230. 'is_delete' => 0,
  231. 'is_use' => 0,
  232. 'is_expire' => 0,
  233. ])->count();
  234. // 获取核销卡数量
  235. $verify_num = VerifyCardSale::find()->where([
  236. 'user_id' => get_user_id(),
  237. 'is_delete' => 0,
  238. ])->count();
  239. /**
  240. * 获取用户可用卡券数量
  241. */
  242. $card_num = UserCard::find()->where([
  243. 'user_id' => get_user_id(),
  244. 'store_id' => get_store_id(),
  245. 'is_use' => 0,
  246. 'is_delete' => 0,
  247. ])->count();
  248. /**
  249. * 获取浏览记录数量(包含商品和店铺的总数)
  250. */
  251. $browse_log_num = BrowseLog::find()->where([
  252. 'user_id' => get_user_id(),
  253. 'store_id' => get_store_id(),
  254. 'is_delete' => 0,
  255. ])->count();
  256. /**
  257. * 获取用户收藏商品数量
  258. */
  259. $favorite_goods_num = Favorite::find()->where([
  260. 'store_id' => get_store_id(),
  261. 'user_id' => get_user_id(),
  262. 'is_delete' => 0,
  263. 'type' => Favorite::FAVORITE_GOODS
  264. ])->count();
  265. if (isset($user) && isset($user['level'])) {
  266. $next_level = Level::find()->where(['store_id' => get_store_id(), 'is_delete' => 0, 'status' => 1])
  267. ->andWhere(['>', 'level', $user['level']])->orderBy(['level' => SORT_ASC, 'id' => SORT_DESC])->asArray()->one();
  268. } else {
  269. $next_level = 0;
  270. }
  271. $shareForm = new ShareForm();
  272. $shareForm->store_id = get_store_id();
  273. $share_setting = $shareForm->getShareSetting()['data'];
  274. $share_msg = $shareForm->getShareMsg();
  275. //我的钱包 选项
  276. $wallet['integral'] = 1;
  277. // $wallet['re'] = User$balance['recharge'];
  278. $wallet['re'] = UserCenterForm::get('recharge_custom_status', get_store_id(), 'recharge', 1);
  279. $order_count = OrderListForm::getCountData(get_store_id(), get_user_id());
  280. // start 获取核销码
  281. // TODO 这里要做支付宝小程序兼容
  282. $code_url = '';
  283. // TODO 影响速度,暂时注释
  284. // if (get_user_id() > 0) {
  285. // if (is_wechat_platform() && !is_platform() && !is_h5()) { // 微信平台
  286. // try {
  287. // $clerkCode = ShareQrcode::wxQrcode('user/clerk/clerk', "user_id:" . get_user_id() . ",store:" . get_store_id());
  288. // if (isset($clerkCode['code']) && $clerkCode['code'] == 0) {
  289. // $code_url = $clerkCode['url_path'];
  290. // }
  291. // } catch (HttpException $e) {
  292. // }
  293. // }
  294. // }
  295. if (isset($data['data']['user_info'])) {
  296. $data['data']['user_info']['is_can_wechat'] = 0;
  297. if (strpos($data['data']['user_info']['nickname'], '****') !== false) {
  298. $data['data']['user_info']['is_can_wechat'] = 1;
  299. }
  300. }
  301. //
  302. $user_model = $user = get_user();
  303. $saas_user_model = SaasUser::findOne(['mobile' => $user_model->binding]);
  304. if(!$saas_user_model->league_price){
  305. $league_price = 0;
  306. }else{
  307. $league_price = $saas_user_model->league_price;
  308. }
  309. $data['data']['user_info']['league_price'] = $league_price;
  310. //是否开启余额转佣金
  311. $balance_to_commission = (int)Option::get('balance_to_commission', get_store_id(), 'recharge', 0)['value'];
  312. $recharge_wallet_status = (int)Option::get('recharge_wallet_status', get_store_id(), 'recharge', 0)['value'];
  313. $balancToCash = (int)Option::get(OptionSetting::BALANCE_TO_CASH, get_store_id(), 'recharge', 0)['value'];
  314. //调用自动升级判断一下
  315. TeamGradesLevel::auto_upgrade(get_user()->id, get_store_id());
  316. $is_team_grades = 0;
  317. $teamGrades = TeamGrades::getUserTeamGrades($user->id);
  318. if (!empty($teamGrades['id'])) {
  319. $is_team_grades = 1;
  320. } else {
  321. $teamGradesLevelList = TeamGradesLevel::getLevelList(get_store_id());
  322. if (!empty($teamGradesLevelList)) {
  323. $is_open_apply = $teamGradesLevelList[0]['is_open_apply'];
  324. if ($is_open_apply) {
  325. $is_team_grades = 2;
  326. }
  327. }
  328. }
  329. $arr = [
  330. 'code' => 0,
  331. 'msg' => 'success',
  332. 'data' => [
  333. 'order_count' => $order_count,
  334. 'show_customer_service' => 1,
  335. 'contact_tel' => UserCenterForm::get('contact_tel', get_store_id(), 'store'),
  336. 'share_setting' => $share_setting,
  337. 'share_msg' => $share_msg,
  338. 'user_info' => isset($data['data']['user_info']) ? $data['data']['user_info'] : null,
  339. 'next_level' => $next_level,
  340. 'orders' => $data['data']['orders'],
  341. 'coupon_num' => $coupon_num,
  342. 'favorite_goods_num' => $favorite_goods_num,
  343. 'card_num' => $card_num,
  344. 'browse_log_num' => $browse_log_num,
  345. 'wallet' => $wallet,
  346. 'code_url' => $code_url,
  347. 'verify_num' => $verify_num,
  348. 'balanc_to_cash' => $balancToCash,
  349. 'balance_to_commission' => $balance_to_commission,
  350. 'recharge_wallet_status' => $recharge_wallet_status,
  351. 'is_team_grades' => $is_team_grades,
  352. ],
  353. ];
  354. return $this->asJson($arr);
  355. }
  356. // 更新公众号用户信息
  357. public function actionUpdateUserInfo()
  358. {
  359. $baseUrl = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl;
  360. $redirect_url = $baseUrl . '/h5/#/pages/my/my';
  361. try {
  362. $code = get_params('code');
  363. Wechat::init(get_store_id(), Wechat::WECHAT_KIND_OFFICIAL, WechatConfig::TYPE_CONFIG_MP);
  364. $result = Wechat::$wechat_official->oauth->userFromCode($code);
  365. $result = $result->toArray();
  366. if (isset($result['raw']['openid'])) {
  367. $user = get_user();
  368. $saas_user = get_saas_user();
  369. if ($user) {
  370. $user->wechat_platform_open_id = $result['raw']['openid'];
  371. if (isset($result['raw']['unionid'])) {
  372. $user->wechat_union_id = $result['raw']['unionid'];
  373. }
  374. if (strpos($user->nickname, '******') !== false) {
  375. $user->nickname = $result['raw']['nickname'];
  376. }
  377. if (strpos($user->avatar_url, 'avatar.png') !== false) {
  378. $user->avatar_url = $result['raw']['headimgurl'];
  379. }
  380. $user->save();
  381. }
  382. if ($saas_user) {
  383. if (strpos($saas_user->name, '******') !== false) {
  384. $saas_user->name = $result['raw']['nickname'];
  385. }
  386. if (strpos($saas_user->avatar, 'avatar.png') !== false) {
  387. $saas_user->avatar = $result['raw']['headimgurl'];
  388. }
  389. $saas_user->save();
  390. }
  391. }
  392. return $this->redirect($redirect_url);
  393. } catch (\Exception $e) {
  394. return $this->redirect($redirect_url);
  395. }
  396. }
  397. /**
  398. * 获取用户信息
  399. */
  400. public function actionGetUserInfo()
  401. {
  402. $saasUser = get_saas_user();
  403. //入驻商提现配置需要灵工提现设置
  404. $lg = Lg::find()->where(['user_id'=>$saasUser->id,'status'=>1,'is_delete'=>0])->one();
  405. if ($lg){
  406. $withdraw_method = json_decode($saasUser->withdraw_method,true);
  407. if ($withdraw_method){
  408. $lg_info['type'] ='lg';
  409. $lg_info['name'] =$lg->name;
  410. $lg_info['account'] =$lg->mobile;
  411. array_push($withdraw_method,$lg_info);
  412. $saasUser->withdraw_method = json_encode($withdraw_method);
  413. }
  414. }
  415. $user = get_user();
  416. return $this->asJson([
  417. 'code' => 0,
  418. 'msg' => 'ok',
  419. 'data' => [
  420. 'saasUser' => $saasUser,
  421. 'user' => $user,
  422. ],
  423. ]);
  424. }
  425. /**
  426. * 修改用户信息
  427. */
  428. public function actionEditUserInfo()
  429. {
  430. // 这里修改为用户商城的头像和昵称
  431. $user = get_user();
  432. if ($user) {
  433. $avatar_url = input_params('avatar_url', '');
  434. $nickname = input_params('nickname', '');
  435. $gender = input_params('gender', '');
  436. $openid = input_params('openid', '');
  437. if (!empty($avatar_url)) {
  438. $user->avatar_url = $avatar_url;
  439. }
  440. if (!empty($nickname)) {
  441. $user->nickname = $nickname;
  442. }
  443. if (!empty($gender) && in_array($gender, [0, 1, 2])) {
  444. $user->gender = $gender;
  445. }
  446. if ($user->save()) {
  447. return $this->asJson([
  448. 'code' => 0,
  449. 'msg' => '保存成功',
  450. ]);
  451. }
  452. return $this->asJson([
  453. 'code' => 1,
  454. 'msg' => '保存失败',
  455. ]);
  456. }
  457. return $this->asJson([
  458. 'code' => 1,
  459. 'msg' => '非法访问',
  460. ]);
  461. }
  462. /**
  463. * 更新用户wechat_app_open_id
  464. */
  465. public function actionUpdateWechatAppOpenId()
  466. {
  467. $id = input_params('user_id');
  468. $openid = input_params('open_id');
  469. if (!$id || !$openid) {
  470. return $this->asJson([
  471. 'code' => 1,
  472. 'msg' => '缺少参数',
  473. ]);
  474. }
  475. $user = User::findOne($id);
  476. if (!$user) {
  477. return $this->asJson([
  478. 'code' => 1,
  479. 'msg' => '用户未找到',
  480. ]);
  481. }
  482. $user->wechat_app_open_id = $openid;
  483. $user->save();
  484. return $this->asJson([
  485. 'code' => 0,
  486. 'msg' => '更新成功',
  487. ]);
  488. }
  489. /**
  490. * 更新用户wechat_app_open_id
  491. */
  492. public function actionUpdateWechatOpenId()
  493. {
  494. if (\Yii::$app->prod_is_dandianpu()) {
  495. $saas_user = get_saas_user();
  496. $binding = $saas_user->mobile;
  497. // $wechat = \Yii::$app->controller->wechat;
  498. $wechat = WechatMini::getWechatConfig(get_store_id());
  499. $code = post_params('code');
  500. $session = $wechat->auth->session($code);
  501. if (!empty($session['openid']) && !empty($binding)) {
  502. User::updateAll(['wechat_open_id' => $session['openid']], ['binding' => $binding]);
  503. }
  504. } else {
  505. $user_id = get_user_id();
  506. $user = User::findOne($user_id);
  507. $wechat = \Yii::$app->controller->wechat;
  508. $code = post_params('code');
  509. $session = $wechat->auth->session($code);
  510. if (!empty($session['openid']) && !empty($user)) {
  511. $user->wechat_open_id = $session['openid'];
  512. if($session['unionid']){
  513. $user->wechat_union_id = $session['unionid'];
  514. }
  515. $user->save();
  516. }
  517. }
  518. }
  519. /**
  520. * Undocumented function
  521. *
  522. * @Author LGL 24963@qq.com
  523. * @DateTime 2021-02-04
  524. * @desc: 绑定手机号
  525. * @return string
  526. */
  527. public function actionUserBinding()
  528. {
  529. $form = new UserForm();
  530. $form->platform = $this->platform;
  531. $form->iv = post_params('iv');
  532. $form->encryptedData = post_params('encryptedData');
  533. $form->code = post_params('code');
  534. return $this->asJson($form->WxBinding());
  535. }
  536. // 授权手机号确认
  537. public function actionUserEmpower()
  538. {
  539. $form = new UserForm();
  540. $form->attributes = post_params();
  541. $form->user_id = get_user_id();
  542. $form->store_id = get_store_id();
  543. return $this->asJson($form->userEmpower());
  544. }
  545. private static function getDefaultIndexData()
  546. {
  547. $form = new UserCenterForm();
  548. $form->store_id = get_store_id();
  549. $data = $form->getData();
  550. return $data['data'];
  551. }
  552. /**
  553. * 获取收货地址
  554. * @return \yii\web\Response
  555. */
  556. public function actionAddressList()
  557. {
  558. $userId = get_params('user_id',0);
  559. if($userId){
  560. $user = User::findOne($userId);
  561. $saasUserId = SaasUser::find()->where(['mobile' => $user->binding])->one()->id;
  562. }
  563. $query = Address::find()->where([
  564. 'is_delete' => 0,
  565. 'user_id' => isset($saasUserId) && $saasUserId > 0 ? $saasUserId : get_saas_user_id(),
  566. ]);
  567. // if (is_platform()) {
  568. // $query->andWhere([
  569. // 'user_id' => get_saas_user_id()
  570. // ]);
  571. // } else {
  572. // $query->andWhere([
  573. // 'user_id' => get_user_id()
  574. // ]);
  575. // }
  576. $list = $query->orderBy('is_default DESC,addtime DESC')->select('id,name,mobile,province_id,province,city_id,city,district_id,village_id,town_id,district,detail,is_default,town,village')->asArray()->all();
  577. foreach ($list as $i => $item) {
  578. $list[$i]['address'] = $item['province'] . $item['city'] . $item['district'] . $item['town'] . $item['village'] . $item['detail'];
  579. }
  580. return $this->asJson([
  581. 'code' => 0,
  582. 'msg' => 'success',
  583. 'data' => [
  584. 'list' => $list,
  585. ],
  586. ]);
  587. }
  588. /**
  589. * 保存收货地址
  590. * @return \yii\web\Response
  591. */
  592. public function actionAddressSave()
  593. {
  594. $form = new AddressSaveForm();
  595. $form->attributes = post_params();
  596. $userId = post_params('user_id',0);
  597. if($userId){
  598. $user = User::findOne($userId);
  599. $saasUserId = SaasUser::find()->where(['mobile' => $user->binding])->one()->id;
  600. }
  601. $form->store_id = get_store_id();
  602. // if (is_platform()) {
  603. $form->user_id = isset($saasUserId) && $saasUserId > 0 ? $saasUserId : get_saas_user_id();
  604. // } else {
  605. // $form->user_id = get_user_id();
  606. // }
  607. return $this->asJson($form->save());
  608. }
  609. /**
  610. * 设为默认收货地址
  611. * @return \yii\web\Response
  612. */
  613. public function actionAddressSetDefault()
  614. {
  615. $form = new AddressSetDefaultForm();
  616. $form->attributes = get_params();
  617. $form->store_id = get_store_id();
  618. // if (is_platform()) {
  619. $form->user_id = get_saas_user_id();
  620. // } else {
  621. // $form->user_id = get_user_id();
  622. // }
  623. return $this->asJson($form->save());
  624. }
  625. /**
  626. * 收货地址详情
  627. * @return \Yii\web\Response
  628. */
  629. public function actionAddressDetail()
  630. {
  631. $form = new AddressSetDefaultForm();
  632. $userId = get_params('user_id',0);
  633. if($userId){
  634. $user = User::findOne($userId);
  635. $saasUserId = SaasUser::find()->where(['mobile' => $user->binding])->one()->id;
  636. }
  637. $form->address_id = get_params('id');
  638. $form->store_id = get_store_id();
  639. // if (is_platform()) {
  640. $form->user_id = isset($saasUserId) && $saasUserId > 0 ? $saasUserId : get_saas_user_id();
  641. // } else {
  642. // $form->user_id = get_user_id();
  643. // }
  644. return $this->asJson($form->info());
  645. }
  646. /**
  647. * 删除收货地址
  648. * @return \yii\web\Response
  649. */
  650. public function actionAddressDelete()
  651. {
  652. $form = new AddressSetDefaultForm();
  653. $form->attributes = get_params();
  654. $form->store_id = get_store_id();
  655. // if (is_platform()) {
  656. $form->user_id = get_saas_user_id();
  657. // } else {
  658. // $form->user_id = get_user_id();
  659. // }
  660. return $this->asJson($form->delete());
  661. }
  662. /**
  663. * 添加商品或店铺或技师到我的喜欢
  664. */
  665. public function actionFavoriteAdd()
  666. {
  667. $form = new FavoriteAddForm();
  668. $form->attributes = post_params();
  669. $form->store_id = get_store_id();
  670. $form->user_id = get_user_id();
  671. return $this->asJson($form->save());
  672. }
  673. /**
  674. * 喜欢的商品或店铺列表
  675. */
  676. public function actionFavoriteList()
  677. {
  678. $form = new FavoriteListForm();
  679. $form->attributes = get_params();
  680. $form->user_id = get_user_id();
  681. $form->store_id = get_store_id();
  682. return $this->asJson($form->search());
  683. }
  684. /**
  685. * 取消喜欢的商品或店铺列表
  686. */
  687. public function actionRemoveFavorite()
  688. {
  689. $id = post_params('id');
  690. if (empty($id)) {
  691. return $this->asJson([
  692. 'code' => 1,
  693. 'msg' => '参数错误'
  694. ]);
  695. }
  696. // if (!is_array($id)) {
  697. // $res = Favorite::updateAll(['is_delete' => 1], ['goods_id' => $id, 'user_id' => get_user_id(), 'type' => post_params('type', 0)]);
  698. // } else {
  699. // $res = Favorite::updateAll(['is_delete' => 1], ['and', ['in', 'goods_id', $id], ['user_id' => get_user_id(), 'type' => post_params('type', 0)]]);
  700. // }
  701. $res = Favorite::updateAll(['is_delete' => 1], ['id' => $id, 'user_id' => get_user_id(), 'type' => post_params('type', 0)]);
  702. if ($res) {
  703. return $this->asJson([
  704. 'code' => 0,
  705. 'msg' => '删除成功',
  706. ]);
  707. } else {
  708. return $this->asJson([
  709. 'code' => 1,
  710. 'msg' => 'fail',
  711. 'err' => $res
  712. ]);
  713. }
  714. }
  715. /**
  716. * 收藏专题列表
  717. */
  718. public function actionTopicFavoriteList()
  719. {
  720. $form = new TopicFavoriteListForm();
  721. $form->attributes = get_params();
  722. $form->store_id = get_store_id();
  723. $form->user_id = get_user_id();
  724. return $this->asJson($form->search());
  725. }
  726. /**
  727. * 收藏|取消收藏专题
  728. */
  729. public function actionTopicFavorite()
  730. {
  731. $form = new TopicFavoriteForm();
  732. $form->attributes = get_params();
  733. $form->store_id = get_store_id();
  734. $form->user_id = get_user_id();
  735. return $this->asJson($form->save());
  736. }
  737. /**
  738. * 用户卡券
  739. * @return \yii\web\Response
  740. */
  741. public function actionCard()
  742. {
  743. $form = new CardListForm();
  744. $form->store_id = $this->store_id;
  745. $form->user_id = get_user_id();
  746. $form->attributes = get_params();
  747. return $this->asJson($form->search());
  748. }
  749. /**
  750. * 卡券二维码
  751. * @return \yii\web\Response
  752. */
  753. public function actionCardQrcode()
  754. {
  755. $user_card_id = get_params('user_card_id');
  756. $form = new QrcodeForm();
  757. $form->page = "user/card-clerk/card-clerk";
  758. $form->width = 100;
  759. $form->scene = "{$user_card_id}";
  760. $form->store_id = $this->store_id;
  761. return $this->asJson($form->getQrcode());
  762. }
  763. /**
  764. * 卡券核销
  765. * @return \yii\web\Response
  766. */
  767. public function actionCardClerk()
  768. {
  769. $user_card_id = get_params('user_card_id');
  770. if (\Yii::$app->cache->get('card_id_' . $user_card_id)) {
  771. return $this->asJson([
  772. 'code' => 1,
  773. 'msg' => '卡券核销中,请稍后重试'
  774. ]);
  775. }
  776. \Yii::$app->cache->set('card_id_' . $user_card_id, true);
  777. $user_card = UserCard::findOne(['id' => $user_card_id]);
  778. if ($user_card->is_use != 0) {
  779. \Yii::$app->cache->set('card_id_' . $user_card_id, false);
  780. return $this->asJson([
  781. 'code' => 1,
  782. 'msg' => '卡券已核销'
  783. ]);
  784. }
  785. $user = get_user();
  786. if ($user->is_clerk != 1) {
  787. \Yii::$app->cache->set('card_id_' . $user_card_id, false);
  788. return $this->asJson([
  789. 'code' => 1,
  790. 'msg' => '不是核销员禁止核销'
  791. ]);
  792. }
  793. $user_card->clerk_id = $user->id;
  794. $user_card->shop_id = $user->shop_id;
  795. $user_card->clerk_time = time();
  796. $user_card->is_use = 1;
  797. if ($user_card->save()) {
  798. \Yii::$app->cache->set('card_id_' . $user_card_id, false);
  799. return $this->asJson([
  800. 'code' => 0,
  801. 'msg' => '核销成功'
  802. ]);
  803. } else {
  804. \Yii::$app->cache->set('card_id_' . $user_card_id, false);
  805. return $this->asJson([
  806. 'code' => 1,
  807. 'msg' => '核销失败'
  808. ]);
  809. }
  810. }
  811. /**
  812. * 卡券详情
  813. * @return \yii\web\Response
  814. */
  815. public function actionCardDetail()
  816. {
  817. $form = new CardListForm();
  818. $form->store_id = $this->store_id;
  819. $form->user_id = get_user_id();
  820. $form->user_card_id = get_params('user_card_id');
  821. return $this->asJson($form->detail());
  822. }
  823. // 短信验证是否开启
  824. public function actionSmsSetting()
  825. {
  826. $option = UserCenterForm::get('user_center_data', get_store_id(), 'store');
  827. // $option = json_decode($option, true);
  828. $option['manual_mobile_auth'] = 1;
  829. if ($option['manual_mobile_auth'] == 1) {
  830. return $this->asJson([
  831. 'code' => 0,
  832. 'msg' => 'success',
  833. 'data' => 1
  834. ]);
  835. } else {
  836. return $this->asJson([
  837. 'code' => 0,
  838. 'msg' => 'success',
  839. 'data' => 0
  840. ]);
  841. }
  842. }
  843. // 会员权益
  844. public function actionMember()
  845. {
  846. $_form = post_params('_form');
  847. $level = is_null(get_user()->level) ? -1 : get_user()->level;
  848. $money = get_user()->money;
  849. $list = Level::find()
  850. ->where(['store_id' => get_store_id(), 'is_delete' => 0, 'status' => 1])
  851. ->orderBy('level asc')->asArray()->all();
  852. foreach ($list as &$val) {
  853. $val['level'] = intval($val['level']);
  854. $val['synopsis'] = $val['synopsis'] == '[]' || !$val['synopsis'] ? [] : json_decode($val['synopsis'], true);
  855. $val['pages_css'] = json_decode($val['pages_css'],true);
  856. }
  857. $now_level = Level::find()->where(['store_id' => get_store_id(), 'level' => $level, 'is_delete' => 0])->asArray()->one();
  858. if ($now_level && $now_level['synopsis']) {
  859. $synopsis = json_decode($now_level['synopsis'], true);
  860. $now_level['synopsis'] = $synopsis;
  861. } else {
  862. $now_level['synopsis'] = [];
  863. }
  864. $user_info = [
  865. 'nickname' => get_saas_user()->name,
  866. 'avatar_url' => get_saas_user()->avatar,
  867. 'id' => get_user()->id,
  868. 'level' => $level,
  869. 'level_name' => !empty($now_level['name']) ? $now_level['name'] : "普通会员",
  870. 'user_level_expires' => get_user()->user_level_expires > 0 ? date('Y年m月d日 H:i:s', get_user()->user_level_expires) : '永久有效',
  871. 'growth_value' => get_user()->growth_value,
  872. ];
  873. $time = time();
  874. $after_sale_time = Option::get('after_sale_time', get_store_id(), 'store');
  875. $sale_time = $time - ($after_sale_time['value'] * 86400);
  876. $next_level = Level::find()->where(['store_id' => get_store_id(), 'is_delete' => 0, 'status' => 1])
  877. ->andWhere(['>', 'level', $level])->orderBy(['level' => SORT_ASC, 'id' => SORT_DESC])->asArray()->one();
  878. $order_money = Order::find()->where(['store_id' => get_store_id(), 'user_id' => get_user_id(), 'is_delete' => 0])
  879. ->andWhere(['is_pay' => 1, 'trade_status' => Order::ORDER_FLOW_CONFIRM])->andWhere(['<=', 'confirm_time', $sale_time])->select([
  880. 'sum(pay_price)'
  881. ])->scalar();
  882. $percent = 100;
  883. $s_money = 0;
  884. $order_money = $order_money ? $order_money : 0;
  885. if ($next_level) {
  886. if ($next_level['money'] != 0) {
  887. $percent = round($order_money / $next_level['money'] * 100, 2);
  888. }
  889. $s_money = round($next_level['money'] - $order_money, 2);
  890. }
  891. $growthValue = UserGrowthValueLog::find()->where(['user_id' => get_user_id(),'from'=>UserGrowthValueLog::TYPE_BUY,'log_type'=>UserGrowthValueLog::LOG_TYPE_ADD])->sum('amount') ?? 0;
  892. $today_day = date('Y-m-d');
  893. $today = Register::find()->where(['register_time' => $today_day, 'store_id' => get_store_id(), 'user_id' => get_user_id()])->asArray()->one();
  894. return $this->asJson([
  895. 'code' => 0,
  896. 'msg' => 'success',
  897. 'data' => [
  898. 'pay_type_list' => $this->getPayTypeList($_form),
  899. 'user_info' => $user_info,
  900. 'next_level' => $next_level,
  901. 'now_level' => $now_level,
  902. 'order_money' => $order_money,
  903. 'percent' => $percent,
  904. 's_money' => $s_money,
  905. 'money' => $money,
  906. 'list' => $list,
  907. 'growth_value' => $growthValue,
  908. 'is_sign' => $today ? 1 : 0
  909. ],
  910. ]);
  911. }
  912. /**
  913. * 会员支付
  914. */
  915. public function actionSubmitMember()
  916. {
  917. $form = new OrderMemberForm();
  918. $form->store_id = get_store_id();
  919. $form->user = get_user();
  920. $form->attributes = post_params();
  921. return $this->asJson($form->save());
  922. }
  923. /**
  924. * 支付
  925. * @return \yii\web\Response
  926. */
  927. public function actionMemberPayData()
  928. {
  929. $form = new LevelPayDataForm();
  930. $form->attributes = get_params();
  931. $form->store_id = get_store_id();
  932. $form->user = get_user();
  933. return $this->asJson($form->search());
  934. }
  935. // 获取支付方式TODO临时
  936. protected function getPayTypeList($_form = '')
  937. {
  938. $pay_type_list_json = Option::get(OptionSetting::STORE_PAYMENT, get_store_id(), 'pay', Option::get(OptionSetting::STORE_PAYMENT, get_store_id(), 'store', '{"wechat":{"text":"微信支付","value":1}}')['value']);
  939. $pay_type_list = Json::decode($pay_type_list_json['value']);
  940. if (!(is_array($pay_type_list) || $pay_type_list instanceof \ArrayObject)) {
  941. return [];
  942. }
  943. $new_list = [];
  944. foreach ($pay_type_list as $index => $value) {
  945. // if ($index == 'wechat' && $value['value'] == 1) {
  946. // $new_list[] = [
  947. // 'name' => '微信支付',
  948. // 'payment' => 0,
  949. // ];
  950. // }
  951. //
  952. // if ($index == 'alipay' && $value['value'] == 1) {
  953. // $new_list[] = [
  954. // 'name' => '支付宝支付',
  955. // 'payment' => 4,
  956. // ];
  957. // }
  958. if ((is_wechat_platform() || $_form === 'app' || $_form === 'h5') && $index == 'wechat' && $value['value'] == 1) {
  959. $new_list[] = [
  960. 'name' => '微信支付',
  961. 'payment' => 1,
  962. // 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-online.png'
  963. ];
  964. }
  965. if ((is_wechat_platform()) && $index == Order::PAY_TYPE_KEY_WX_B2B && $value['value'] == 1) {
  966. $new_list[] = [
  967. 'name' => Order::PAY_TYPE_NAME_WX_B2B,
  968. 'payment' => Order::PAY_TYPE_WX_B2B,
  969. 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-online.png'
  970. ];
  971. }
  972. if ((is_alipay_platform() || $_form === 'app') && $index == 'alipay' && $value['value'] == 1) {
  973. $new_list[] = [
  974. 'name' => '支付宝支付',
  975. 'payment' => 4,
  976. // 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-alipay.png'
  977. ];
  978. }
  979. if ($index == 'balance' && $value['value'] == 1) {
  980. $balance = Option::get('recharge_wallet_status', get_store_id(), 'recharge');
  981. if ($balance && $balance['value'] == 1) {
  982. $new_list[] = [
  983. 'name' => '余额支付',
  984. 'payment' => 3,
  985. ];
  986. }
  987. }
  988. }
  989. debug_log($new_list, 'pay_type.log');
  990. return $new_list;
  991. }
  992. /**
  993. * 修改saas user信息
  994. * @return \yii\web\Response
  995. */
  996. public function actionSaasUserModify() {
  997. $form = new SaasUserForm();
  998. $form->mobile = post_params('mobile');
  999. $form->attributes = post_params();
  1000. return $this->asJson($form->modify());
  1001. }
  1002. public function actionBalanceToCommission()
  1003. {
  1004. $price = (float)post_params('price');
  1005. if ($price <= 0) {
  1006. return $this->asJson([
  1007. 'code' => 1,
  1008. 'msg' => '参数错误',
  1009. ]);
  1010. }
  1011. $user = get_user();
  1012. $store_id = get_store_id();
  1013. if ($user->is_distributor != 1) {
  1014. return $this->asJson([
  1015. 'code' => 1,
  1016. 'msg' => '您还不是分销商,暂不能把余额转到佣金',
  1017. ]);
  1018. }
  1019. if ($price > $user->money) {
  1020. return $this->asJson([
  1021. 'code' => 1,
  1022. 'msg' => '待转的余额不能大于已有余额',
  1023. ]);
  1024. }
  1025. $transaction = \Yii::$app->db->beginTransaction();
  1026. try {
  1027. $money = $user->money;
  1028. $user->money -= $price;
  1029. $user->price += $price;
  1030. $user->total_price += $price;
  1031. $user->save();
  1032. $log = new AccountLog();
  1033. $log->store_id = $store_id;
  1034. $log->user_id = $user->id;
  1035. $log->type = AccountLog::TYPE_BALANCE;
  1036. $log->log_type = AccountLog::LOG_TYPE_EXPEND;
  1037. $log->amount = $price;
  1038. $log->desc = '余额转分销佣金';
  1039. $log->before = $money;
  1040. $log->after = $user->money;
  1041. $log->operator = '';
  1042. $log->operator_id = 0;
  1043. $log->order_type = AccountLog::TYPE_BALANCE_TO_COMMISSION;
  1044. $log->order_id = 0;
  1045. $log->operator_type = AccountLog::TYPE_OPERATOR_NORMAL;
  1046. $log->created_at = time();
  1047. $res = $log->save();
  1048. if ($res) {
  1049. $transaction->commit();
  1050. return $this->asJson([
  1051. 'code' => 0,
  1052. 'msg' => '操作成功',
  1053. ]);
  1054. }
  1055. $transaction->rollBack();
  1056. return $this->asJson([
  1057. 'code' => 1,
  1058. 'msg' => '操作失败!',
  1059. ]);
  1060. } catch (\Exception $e) {
  1061. \Yii::error($e->getMessage());
  1062. $transaction->rollBack();
  1063. return $this->asJson([
  1064. 'code' => 1,
  1065. 'msg' => '操作失败!',
  1066. ]);
  1067. }
  1068. }
  1069. public function actionCodeToOpenid() {
  1070. $code = post_params('code', '');
  1071. if (empty($code)) {
  1072. return $this->asJson([
  1073. 'code' => 1,
  1074. 'msg' => 'code为空'
  1075. ]);
  1076. }
  1077. try {
  1078. Wechat::init(get_store_id(), Wechat::WECHAT_KIND_OFFICIAL, WechatConfig::TYPE_CONFIG_MP);
  1079. $result = Wechat::$wechat_official->oauth->userFromCode($code);
  1080. if ($result && $result->getId()) {
  1081. \Yii::error($result->getRaw());
  1082. $raw = $result->getRaw();
  1083. $user = get_user();
  1084. $user->wechat_platform_open_id = $result->getId();
  1085. if ($raw['unionid']) {
  1086. $user->wechat_union_id = $raw['unionid'];
  1087. }
  1088. if (strpos($user->nickname, '******') !== false && $result->getNickname()) {
  1089. $user->nickname = $result->getNickname();
  1090. }
  1091. if (strpos($user->avatar_url, 'avatar.png') !== false && $raw['headimgurl']) {
  1092. $user->avatar_url = $raw['headimgurl'];
  1093. }
  1094. if ($user->save()) {
  1095. $saas_user = get_saas_user();
  1096. if (strpos($saas_user->name, '******') !== false) {
  1097. $saas_user->name = $result->getNickname();
  1098. }
  1099. if (strpos($saas_user->avatar, 'avatar.png') !== false) {
  1100. $saas_user->avatar = $raw['headimgurl'];
  1101. }
  1102. $saas_user->save();
  1103. return $this->asJson([
  1104. 'code' => 0,
  1105. 'msg' => '授权成功'
  1106. ]);
  1107. }
  1108. }
  1109. return $this->asJson([
  1110. 'code' => 1,
  1111. 'msg' => '授权失败'
  1112. ]);
  1113. } catch (\Exception $e) {
  1114. return $this->asJson([
  1115. 'code' => 1,
  1116. 'msg' => $e->getMessage()
  1117. ]);
  1118. }
  1119. }
  1120. //确定代理已经安装
  1121. public function actionAgentIsInstall() {
  1122. $t = \Yii::$app->db->beginTransaction();
  1123. try {
  1124. $order_detail_id = get_params('order_detail_id');
  1125. $order_detail = OrderDetail::findOne($order_detail_id);
  1126. if (empty($order_detail)) {
  1127. throw new \Exception("订单未找到");
  1128. }
  1129. $agent_install_log = AgentGoodsInstallLog::findOne(['order_detail_id' => $order_detail_id, 'order_type' => AgentGoodsInstallLog::ORDER_TYPE_NORMAL]);
  1130. if (!in_array($agent_install_log->status, [0, 1])) {
  1131. throw new \Exception("已经操作完成,不可重复操作");
  1132. }
  1133. $agent_install_log->status = 2;
  1134. if (!$agent_install_log->save()) {
  1135. throw new \Exception(json_encode($agent_install_log->errors));
  1136. }
  1137. //判断是否全部确认收货
  1138. $install_all_log = AgentGoodsInstallLog::find()->where(['AND', ['order_id' => $agent_install_log->order_id, 'order_type' => AgentGoodsInstallLog::ORDER_TYPE_NORMAL]])->asArray()->all();
  1139. if ($install_all_log) {
  1140. $count = OrderDetail::find()->where(['order_id' => $agent_install_log->order_id])->count();
  1141. if (count($install_all_log) >= $count) {
  1142. $order = Order::findOne($agent_install_log->order_id);
  1143. $order->trade_status = Order::ORDER_FLOW_CONFIRM;
  1144. if (!$order->save()) {
  1145. throw new \Exception("订单信息错误");
  1146. }
  1147. }
  1148. }
  1149. //判断是否已过售后时间
  1150. $time = time();
  1151. $after_sale_time = Option::get(OptionSetting::STORE_AFTER_SALE_TIME, $this->store_id)['value'];
  1152. $sale_time = $time - ($after_sale_time * 86400);
  1153. $order = Order::find()->alias('o')
  1154. ->where([
  1155. 'and',
  1156. ['o.store_id' => get_store_id()],
  1157. ['o.is_delete' => 0, 'o.trade_status' => Order::ORDER_FLOW_CONFIRM],
  1158. ['<=', 'o.confirm_time', $sale_time],
  1159. ['o.id' => $order_detail->order_id],
  1160. ])->andWhere(['<>', 'trade_status', Order::ORDER_FLOW_CANCEL])
  1161. ->leftJoin(OrderRefund::tableName() . ' r', "r.order_id = o.id and r.is_delete = 0")
  1162. ->select(['o.*'])->groupBy('o.id')
  1163. ->andWhere([
  1164. 'or',
  1165. 'isnull(r.id)',
  1166. ['r.type' => 2],
  1167. ['in', 'r.status', [2, 3]],
  1168. ])->asArray()->one();
  1169. if (!empty($order) && (int)$order['is_sale'] === 1) {
  1170. $goods = Goods::findOne($order_detail->goods_id);
  1171. if (!(!empty($goods) && $goods->cloud_goods_id)) {
  1172. throw new \Exception('非云仓商品,不可操作');
  1173. }
  1174. $agent_goods_bind_goods = AgentGoodsBindGoods::findOne(['cloud_goods_id' => $goods->cloud_goods_id]);
  1175. if (empty($agent_goods_bind_goods)) {
  1176. throw new \Exception('未绑定产品代理,不可操作');
  1177. }
  1178. $option = Option::get('goods_agent_price_config', 0, 'saas', [])['value'];
  1179. $option = json_decode($option, true);
  1180. $goods_agent_install_percent = $option['goods_agent_install_percent'] ?? 0;
  1181. $supplier = Supplier::findOne(['cloud_supplier_id' => $agent_goods_bind_goods->cloud_supplier_id]);
  1182. if (empty($supplier)) {
  1183. throw new \Exception('供货商查询失败,不可操作');
  1184. }
  1185. $supplier_rate = $supplier->rate;
  1186. //计算上门安装佣金
  1187. $admin = Admin::findOne($agent_install_log->goods_agent_admin_id);
  1188. if (!empty($admin)) {
  1189. $agent_rebate = $order_detail->total_price * ($supplier_rate / 100) * ($goods_agent_install_percent / 100);
  1190. //给用户增加佣金
  1191. $SaasUser = SaasUser::findOne($admin->saas_user_id);
  1192. $SaasUser->share_profit = ($SaasUser->share_profit * 1) + ($agent_rebate * 1);
  1193. if (!$SaasUser->save()) {
  1194. throw new \Exception(json_encode($SaasUser->errors));
  1195. }
  1196. $StoreShareMoney = new StoreShareMoney();
  1197. $StoreShareMoney->user_id = $SaasUser->id;
  1198. $StoreShareMoney->store_id = $order['store_id'];
  1199. $StoreShareMoney->profit = $goods_agent_install_percent;
  1200. $StoreShareMoney->total_price = $order_detail->total_price;
  1201. $StoreShareMoney->desc = '产品代理上门安装佣金发放,安装商品' . $order_detail->goods_name;
  1202. $StoreShareMoney->order_id = $order['id'];
  1203. $StoreShareMoney->created_at = time();
  1204. $StoreShareMoney->status = StoreShareMoney::STATUS_STORE_ORDER;
  1205. $StoreShareMoney->type = StoreShareMoney::TYPE_GOODS_AGENT;
  1206. $StoreShareMoney->commission = $agent_rebate;
  1207. $StoreShareMoney->is_send = 1;
  1208. if (!$StoreShareMoney->save()) {
  1209. throw new \Exception(json_encode($StoreShareMoney->errors));
  1210. }
  1211. }
  1212. }
  1213. $t->commit();
  1214. return $this->asJson([
  1215. 'code' => 0,
  1216. 'msg' => '操作成功'
  1217. ]);
  1218. } catch (\Exception $e) {
  1219. $t->rollBack();
  1220. return $this->asJson([
  1221. 'code' => 1,
  1222. 'msg' => $e->getMessage()
  1223. ]);
  1224. }
  1225. }
  1226. public function actionCommissionToBalance() {
  1227. $transaction = \Yii::$app->db->beginTransaction();
  1228. try {
  1229. $price = post_params('price');
  1230. if (empty($price)) {
  1231. return $this->asJson([
  1232. 'code' => 1,
  1233. 'msg' => '缺少必要参数',
  1234. ]);
  1235. }
  1236. if ($price <= 0) {
  1237. return $this->asJson([
  1238. 'code' => 1,
  1239. 'msg' => '佣金数量数据错误',
  1240. ]);
  1241. }
  1242. $user = get_user();
  1243. $store_id = get_store_id();
  1244. if ($user->is_distributor != 1) {
  1245. return $this->asJson([
  1246. 'code' => 1,
  1247. 'msg' => '您还不是分销商,暂不能把佣金转到余额',
  1248. ]);
  1249. }
  1250. if ($price > $user->price) {
  1251. return $this->asJson([
  1252. 'code' => 1,
  1253. 'msg' => '待转的佣金不能大于已有佣金数额',
  1254. ]);
  1255. }
  1256. $money = $user->money;
  1257. // $user->money += $price;
  1258. //计算手续费
  1259. $profit = Option::get('commission_to_balance', get_store_id(), 'recharge', 0)['value'];
  1260. if ($profit) {
  1261. $profit = $price * ($profit / 100);
  1262. }
  1263. $user->price -= $price;
  1264. $user->save();
  1265. if ($profit) {
  1266. $price = sprintf("%.2f", ($price - $profit));
  1267. }
  1268. AccountLog::saveLog($user->id, $price, 2, 1, 0, 0, '分销佣金转余额,手续费:' . $profit . '元', 3, $user->id, $profit);
  1269. $transaction->commit();
  1270. return $this->asJson([
  1271. 'code' => 0,
  1272. 'msg' => '操作成功',
  1273. ]);
  1274. } catch (\Exception $e) {
  1275. $transaction->rollBack();
  1276. return $this->asJson([
  1277. 'code' => 1,
  1278. 'msg' => $e->getMessage()
  1279. ]);
  1280. }
  1281. }
  1282. //积分 / 余额转赠
  1283. public function actionGiveAssets() {
  1284. $t = \Yii::$app->db->beginTransaction();
  1285. try {
  1286. $user = get_user();
  1287. $saas_user = get_saas_user();
  1288. $give_user_id = post_params('give_user_id');
  1289. $money = post_params('money', 0);
  1290. $type = post_params('type', 2); // 2 余额 1 积分
  1291. if (!in_array($type, [1, 2])) {
  1292. throw new \Exception('参数类型错误');
  1293. }
  1294. $give_user = User::find()->alias('u')
  1295. ->leftJoin(['su' => SaasUser::tableName()], 'u.binding = su.mobile')
  1296. ->where(['u.id' => $give_user_id, 'u.is_delete' => 0])
  1297. ->select('u.id, su.name')->asArray()->one();
  1298. if (!$give_user) {
  1299. throw new \Exception('用户查找失败');
  1300. }
  1301. if ((int)$type === 2) {
  1302. //计算手续费
  1303. $profit = Option::get('give_balance_profit', get_store_id(), 'recharge', 0)['value'];
  1304. if ($user->money < $money) {
  1305. throw new \Exception('可转赠余额不足');
  1306. }
  1307. } else {
  1308. //计算手续费
  1309. $profit = Option::get('give_integral_profit', get_store_id(), 'recharge', 0)['value'];
  1310. if ($user->integral < $money) {
  1311. throw new \Exception('可转赠积分数量不足');
  1312. }
  1313. }
  1314. if ($money > 0) {
  1315. $profit = (float)sprintf("%.2f", $money * (($profit ?: 0) / 100));
  1316. AccountLog::saveLog($user->id, $money, $type, 2, 0, 0, '转赠给用户' . $give_user['name'] . ',手续费' . $profit, 1, $give_user['id'], $profit);
  1317. $money = (float)sprintf("%.2f", ($money - $profit));
  1318. AccountLog::saveLog($give_user['id'], $money, $type, 1, 0, 0, '用户' . $saas_user->name . '转赠', 2, $user->id, $profit);
  1319. }
  1320. $t->commit();
  1321. return $this->asJson([
  1322. 'code' => 0,
  1323. 'msg' => '操作成功'
  1324. ]);
  1325. } catch (\Exception $e) {
  1326. $t->rollBack();
  1327. return $this->asJson([
  1328. 'code' => 1,
  1329. 'msg' => $e->getMessage() . $e->getLine()
  1330. ]);
  1331. }
  1332. }
  1333. //查询用户
  1334. public function actionSearchUser() {
  1335. try {
  1336. $mobile = get_params('mobile');
  1337. if ($mobile) {
  1338. $give_user = User::find()->alias('u')
  1339. ->leftJoin(['su' => SaasUser::tableName()], 'u.binding = su.mobile')
  1340. ->where(['u.is_delete' => 0, 'u.store_id' => get_store_id()])
  1341. ->andWhere(['OR', ['u.id' => $mobile], ['u.binding' => $mobile]])
  1342. ->select('u.id, su.name, su.avatar')->asArray()->one();
  1343. if (empty($give_user)) {
  1344. $give_user = null;
  1345. }
  1346. if ((int)$give_user['id'] === (int)get_user_id()) {
  1347. $give_user = null;
  1348. }
  1349. } else {
  1350. $give_user = null;
  1351. }
  1352. return $this->asJson([
  1353. 'code' => 0,
  1354. 'msg' => 'success',
  1355. 'data' => $give_user
  1356. ]);
  1357. } catch (\Exception $e) {
  1358. return $this->asJson([
  1359. 'code' => 1,
  1360. 'msg' => $e->getMessage()
  1361. ]);
  1362. }
  1363. }
  1364. //获取佣金 余额 积分 转赠记录
  1365. public function actionGetAccountLog() {
  1366. try {
  1367. $type = (int)get_params('type', 0);
  1368. $log_type = get_params('log_type');
  1369. $page = get_params('page', 1);
  1370. $limit = get_params('limit', 10);
  1371. $user_id = get_user_id();
  1372. $store_id = get_store_id();
  1373. switch ($type) {
  1374. case 1: //积分 且 赠送
  1375. $type = 2;
  1376. $from = [1, 2];
  1377. break;
  1378. case 2://余额 且 佣金转
  1379. $type = 2;
  1380. $from = 3;
  1381. break;
  1382. default: //余额 且 赠送
  1383. $type = 1;
  1384. $from = [1, 2];
  1385. break;
  1386. }
  1387. $query = AccountLog::find()->where([
  1388. 'store_id' => $store_id,
  1389. 'user_id' => $user_id,
  1390. 'type' => $type, // 余额
  1391. 'from' =>$from
  1392. ]);
  1393. if (in_array($log_type, [1, 2])) {
  1394. $query->andWhere(['log_type' => $log_type]);
  1395. }
  1396. $count = $query->count();
  1397. $pagination = new Pagination(['totalCount' => $count, 'page' => $page - 1, 'pageSize' => $limit]);
  1398. $list = $query->limit($pagination->limit)->offset($pagination->offset)->orderBy('created_at DESC')->asArray()->all();
  1399. foreach ($list as $key => &$value) {
  1400. $value['date'] = date('Y-m-d H:i:s', $value['created_at']);
  1401. $value['content'] = $value['desc'];
  1402. }
  1403. return $this->asJson([
  1404. 'code' => 0,
  1405. 'msg' => 'success',
  1406. 'data' => [
  1407. 'list' => $list,
  1408. 'page_count' => $pagination->getPageCount(),
  1409. 'row_count' => $count,//总数
  1410. ]
  1411. ]);
  1412. }catch (\Exception $e) {
  1413. return $this->asJson([
  1414. 'code' => 1,
  1415. 'msg' => $e->getMessage()
  1416. ]);
  1417. }
  1418. }
  1419. //获取手续费
  1420. public function actionGetProfit() {
  1421. //计算手续费
  1422. $commission_to_balance = Option::get('commission_to_balance', get_store_id(), 'recharge', 0)['value'];
  1423. //计算手续费
  1424. $give_balance_profit = Option::get('give_balance_profit', get_store_id(), 'recharge', 20)['value'];
  1425. //计算手续费
  1426. $give_integral_profit = Option::get('give_integral_profit', get_store_id(), 'recharge', 25)['value'];
  1427. return $this->asJson([
  1428. 'code' => '0',
  1429. 'msg' => '获取成功',
  1430. 'data' => [
  1431. "commission_to_balance" => $commission_to_balance,
  1432. "give_balance_profit" => $give_balance_profit,
  1433. "give_integral_profit" => $give_integral_profit
  1434. ]
  1435. ]);
  1436. }
  1437. /**
  1438. * 修改手机号
  1439. */
  1440. public function actionUpdateMobile()
  1441. {
  1442. $t = \Yii::$app->db->beginTransaction();
  1443. try {
  1444. $store_id = get_store_id();
  1445. $mobile = \input_params('mobile');
  1446. $code = \input_params('code');
  1447. if (!$mobile || !$code) {
  1448. throw new \Exception('参数错误');
  1449. }
  1450. $form = new LoginForm();
  1451. $form->store_id = $store_id;
  1452. $form->phone = $mobile;
  1453. $form->verify_code = $code;
  1454. $verifySmsCode = $form->verifySmsCode(LoginForm::CACHE_KEY_BIND_PHONE);
  1455. if ($verifySmsCode['code'] != 0) {
  1456. throw new \Exception($verifySmsCode['msg']);
  1457. }
  1458. $is = User::findOne(['binding' => $mobile, 'is_delete' => 0]);
  1459. if ($is) {
  1460. throw new \Exception('该手机号已被绑定');
  1461. }
  1462. $currentUser = get_user();
  1463. $users = User::find()->where(['binding' => $currentUser->binding, 'is_delete' => 0])->all();
  1464. foreach ($users as $user) {
  1465. $user->binding = $mobile;
  1466. $user->save();
  1467. }
  1468. $saasUser = SaasUser::findOne(['mobile' => $currentUser->binding, 'is_delete' => 0]);
  1469. if ($saasUser) {
  1470. $saasUser->mobile = $mobile;
  1471. $saasUser->save();
  1472. }
  1473. $t->commit();
  1474. return $this->asJson([
  1475. 'code' => 0,
  1476. 'msg' => '修改成功',
  1477. ]);
  1478. } catch (\Exception $e) {
  1479. $t->rollBack();
  1480. return $this->asJson([
  1481. 'code' => 1,
  1482. 'msg' => $e->getMessage()
  1483. ]);
  1484. }
  1485. }
  1486. /**
  1487. * 模块名:actionUpdateRemarkName
  1488. * 代码描述:修改用户备注
  1489. * 作者:WPing丶
  1490. * 请求方式:GET
  1491. * 创建时间:2024/03/14 10:07:25
  1492. * @param int id 用户ID
  1493. * @param string remark_name 备注名
  1494. */
  1495. public function actionUpdateRemarkName() {
  1496. $id = get_params('id', 0);
  1497. $remark_name = get_params('remark_name', 0);
  1498. $user_id = get_user_id();
  1499. $store_id = get_store_id();
  1500. $user_info = User::findOne(['id'=>$id, 'store_id' => $store_id]);
  1501. if(!$user_info) {
  1502. return $this->asJson([
  1503. 'code' => 1,
  1504. 'msg' => '用户不存在'
  1505. ]);
  1506. }
  1507. if($user_info->old_parent_id != $user_id) {
  1508. return $this->asJson([
  1509. 'code' => 1,
  1510. 'msg' => '您不是此用户的推荐人,无法修改备注'
  1511. ]);
  1512. }
  1513. $user_info->remark_name = $remark_name;
  1514. if(!$user_info->save()) {
  1515. return $this->asJson([
  1516. 'code' => 1,
  1517. 'msg' => '保存User表失败'
  1518. ]);
  1519. } else {
  1520. return $this->asJson([
  1521. 'code' => 0,
  1522. 'msg' => '修改成功',
  1523. ]);
  1524. }
  1525. }
  1526. /**
  1527. * 模块名:actionCheckStudent
  1528. * 代码描述:快速获取学生身份API
  1529. * 作者:WPing丶
  1530. * 请求方式:GET
  1531. * 创建时间:2024/03/18 11:14:42
  1532. * @param string wx_studentcheck_code 用户授权查询code
  1533. */
  1534. public function actionCheckStudent() {
  1535. $wx_studentcheck_code = get_params('wx_studentcheck_code', 0);
  1536. $user_id = get_user_id();
  1537. $store_id = get_store_id();
  1538. $user_info = User::findOne(['id' => $user_id, 'store_id' => $store_id]);
  1539. if(!$user_info) {
  1540. return $this->asJson([
  1541. 'code' => 1,
  1542. 'msg' => '用户不存在',
  1543. ]);
  1544. }
  1545. $app = (new WechatMini())::getWechatConfig($store_id);
  1546. if (!$app) {
  1547. throw new \Exception('获取小程序信息失败');
  1548. }
  1549. $client = new BaseClient($app);
  1550. $params_ = [
  1551. 'openid' => $user_info->wechat_open_id,
  1552. 'wx_studentcheck_code' => $wx_studentcheck_code,
  1553. ];
  1554. $result = $client->httpPostJson('intp/quickcheckstudentidentity',$params_);
  1555. if($result['is_student']) {
  1556. //判断是否设置学生会员
  1557. $student_level = Level::findOne(['is_student' => 1, 'status' => 1, 'is_delete' => 0]);
  1558. if($student_level && $student_level->level > $user_info->level) {
  1559. $user_info->level = $student_level->level;
  1560. }
  1561. $user_info->is_student = 1;
  1562. if(!$user_info->save()) {
  1563. return $this->asJson([
  1564. 'code' => 1,
  1565. 'msg' => '保存用户信息失败',
  1566. 'data' => [
  1567. 'store_id' => $store_id,
  1568. 'user_id' => $user_id,
  1569. 'user_info' => $user_info,
  1570. 'error_msg' => $user_info->getErrors(),
  1571. ]
  1572. ]);
  1573. }
  1574. }
  1575. $result['code'] = $result['errcode'] == 0 ? 0 : 1;
  1576. $result['msg'] = $result['errmsg'];
  1577. return $this->asJson($result);
  1578. }
  1579. /**
  1580. * 根据用户手机号生成条形码以及二维码获取手机号
  1581. */
  1582. public function actionGetUserMobileQrcode() {
  1583. $user = get_user();
  1584. $mobile = $user->binding;
  1585. $file_name = md5($mobile . '_' . get_store_id() . '_user_mobile_qrcode'). '.jpg';
  1586. // 保存小程序码到文件
  1587. $dir = \Yii::$app->runtimePath . '/image/wx_qrcode';
  1588. if (! is_dir($dir)) {
  1589. mkdir($dir, 0777, true);
  1590. }
  1591. $url = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/runtime/image/wx_qrcode/' . $file_name);
  1592. QrCode::image($mobile, 600, false, 'L', 'JPEG', 0, ['255,255,255', '0,0,0'], 1, false, $dir. '/' .$file_name );
  1593. $generator = new BarcodeGeneratorJPG();
  1594. $barcode = $generator->getBarcode($mobile, $generator::TYPE_CODE_128, 2, 30);
  1595. $file_name = md5($mobile . '_' . get_store_id() . '_user_mobile_barcode'). '.jpg';
  1596. $code_path = \Yii::$app->runtimePath . '/image/' . $file_name;
  1597. $pic_url = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/runtime/image/' . $file_name);
  1598. file_put_contents($code_path, $barcode);
  1599. return $this->asJson([
  1600. 'code' => 0,
  1601. 'msg' => '',
  1602. 'data' => [
  1603. 'user_qrcode' => $url,
  1604. 'user_barcode' => $pic_url,
  1605. 'mobile' => $mobile
  1606. ]
  1607. ]);
  1608. }
  1609. public function actionUserAuditSetting() {
  1610. $userAuditSetting = UserAuditSetting::findOne(['store_id' => get_store_id()]);
  1611. return $this->asJson([
  1612. 'code' => 0,
  1613. 'data' => $userAuditSetting,
  1614. ]);
  1615. }
  1616. public function actionWxCode2session() {
  1617. $code = input_params('code');
  1618. [$wechat_open_id, $ali_user_id, $ali_open_id, $session_key] = (new UserForm())->getOpenId($code);
  1619. return $this->asJson([
  1620. 'code' => 0,
  1621. 'data' => [
  1622. 'session_key' => $session_key,
  1623. ],
  1624. 'res' => [$wechat_open_id, $ali_user_id, $ali_open_id, $session_key],
  1625. ]);
  1626. }
  1627. /**
  1628. * 获取申请信息
  1629. */
  1630. public function actionUserAudit() {
  1631. try {
  1632. $user = get_user();//获取当前用户信息
  1633. $saasUser = get_saas_user();
  1634. $store_id = get_store_id();//获取商城id
  1635. $avatar = post_params('avatar');//获取头像信息
  1636. $custom_form = post_params('custom_form');//获取自定义表单信息
  1637. $mobile = post_params('mobile');
  1638. $userAuditSetting = UserAuditSetting::findOne(['store_id' => $store_id]);//获取当前商城用户审核的设置
  1639. $session_key = post_params('session_key', '');
  1640. $wechat_open_id = post_params('wechat_open_id', '');
  1641. $ali_user_id = post_params('ali_user_id', '');
  1642. $ali_open_id = post_params('ali_open_id', '');
  1643. $code = get_params('code');
  1644. if (!$user) {
  1645. //用户未登陆时 获取openid来分辨用户
  1646. [$wechat_open_id, $ali_user_id, $ali_open_id, $session_key] = (new UserForm())->getOpenId($code, [
  1647. 'wechat_open_id' => $wechat_open_id,
  1648. 'ali_user_id' => $ali_user_id,
  1649. 'ali_open_id' => $ali_open_id,
  1650. ]);
  1651. }
  1652. if (!empty($wechat_open_id)) {
  1653. $user = User::findOne(['store_id' => $store_id, 'wechat_open_id' => $wechat_open_id, 'is_delete' => 0]);
  1654. if ($user && $user->binding) {
  1655. $saasUser = SaasUser::findOne(['is_delete' => 0, 'mobile' => $user->binding]);
  1656. }
  1657. }
  1658. if (!empty($ali_user_id)) {
  1659. $user = User::findOne(['store_id' => $store_id, 'alipay_open_id' => $ali_user_id, 'is_delete' => 0]);
  1660. if ($user) {
  1661. if ($user->binding) {
  1662. $saasUser = SaasUser::findOne(['is_delete' => 0, 'mobile' => $user->binding]);
  1663. } else {
  1664. $saasUser = SaasUser::findOne(['is_delete' => 0, 'ali_user_id' => $ali_user_id]);
  1665. }
  1666. }
  1667. }
  1668. if (!empty($ali_open_id)) {
  1669. $user = User::findOne(['store_id' => $store_id, 'ali_openId' => $ali_open_id, 'is_delete' => 0]);
  1670. if ($user) {
  1671. if ($user->binding) {
  1672. $saasUser = SaasUser::findOne(['is_delete' => 0, 'mobile' => $user->binding]);
  1673. } else {
  1674. $saasUser = SaasUser::findOne(['is_delete' => 0, 'ali_openId' => $ali_open_id]);
  1675. }
  1676. }
  1677. }
  1678. if ($user) {
  1679. $userAuditLog = UserAuditLog::findOne(['user_id' => $user->id, 'is_delete' => 0]);//获取当前用户审核记录
  1680. } else {
  1681. $userAuditLog = UserAuditLog::find()->where(['store_id' => $store_id, 'is_delete' => 0]);
  1682. if ($wechat_open_id || $ali_user_id || $ali_open_id) {
  1683. if ($wechat_open_id) {
  1684. $userAuditLog->andWhere(['wechat_open_id' => $wechat_open_id]);
  1685. }
  1686. if ($ali_user_id) {
  1687. $userAuditLog->andWhere(['ali_user_id' => $ali_user_id]);
  1688. }
  1689. if ($ali_open_id) {
  1690. $userAuditLog->andWhere(['ali_openId' => $ali_open_id]);
  1691. }
  1692. $userAuditLog = $userAuditLog->one();
  1693. } else {
  1694. $userAuditLog = null;
  1695. }
  1696. }
  1697. if (\Yii::$app->request->isGet) {//判断是否是get请求
  1698. $custom_form_str = $userAuditSetting->custom_form;
  1699. $custom_form = [];
  1700. if ($custom_form_str) {
  1701. $custom_form = json_decode($custom_form_str, true);
  1702. }
  1703. if ($userAuditLog) {
  1704. $data = [
  1705. 'id' => $userAuditLog->id,
  1706. 'avatar' => $userAuditLog->avatar ?: $saasUser->avatar,
  1707. 'status' => intval($userAuditLog->status),
  1708. 'pass_remark' => $userAuditLog->pass_remark,
  1709. 'mobile' => $userAuditLog->mobile ?: ''
  1710. ];
  1711. if ($userAuditLog->status == UserAuditLog::STATUS_AGREE) {
  1712. $data['token'] = $saasUser->access_token;
  1713. $data['session_key'] = $session_key ?: '';
  1714. }
  1715. } else {
  1716. $data = [
  1717. 'id' => 0,
  1718. 'avatar' => $saasUser->avatar,
  1719. 'status' => UserAuditLog::STATUS_NOT_AUDIT,
  1720. 'pass_remark' => '',
  1721. 'token' => null,
  1722. 'mobile' => $saasUser->mobile ?: ''
  1723. ];
  1724. }
  1725. $user_custom_form = [];
  1726. if (!empty($userAuditLog->custom_form)) {
  1727. $user_custom_form_str = $userAuditLog->custom_form;
  1728. if ($user_custom_form_str) {
  1729. $user_custom_form = json_decode($user_custom_form_str, true);
  1730. }
  1731. }
  1732. $data = array_merge($data, [
  1733. 'custom_form' => $user_custom_form ?: $custom_form,
  1734. 'bg_image' => $userAuditSetting->bg_image ?: '',
  1735. 'protocol' => $userAuditSetting->protocol,
  1736. 'form_name' => $userAuditSetting->form_name,
  1737. 'open_params' => [
  1738. 'wechat_open_id' => $wechat_open_id,
  1739. 'ali_user_id' => $ali_user_id,
  1740. 'ali_open_id' => $ali_open_id,
  1741. ]
  1742. ]);
  1743. if (!empty($data['custom_form'])) {
  1744. foreach ($data['custom_form'] as &$custom_item) {
  1745. if (in_array($custom_item['type'], ['radio', 'checkbox']) && !empty($custom_item['default'])) {
  1746. $custom_item['default_list'] = explode(',', $custom_item['default']);
  1747. $list = explode(',', $custom_item['default']);
  1748. $list_arr = [];
  1749. foreach ($list as $list_index => &$list_item) {
  1750. $arr = [];
  1751. $arr['name'] = $list_item;
  1752. $arr['checked'] = true;
  1753. if ($list_index) {
  1754. $arr['checked'] = false;
  1755. }
  1756. array_push($list_arr, $arr);
  1757. }
  1758. $custom_item['default_list'] = $list_arr;
  1759. }
  1760. $custom_item['default'] = $custom_item['default'] ?: '';
  1761. }
  1762. }
  1763. return $this->asJson([
  1764. 'code' => 0,
  1765. 'msg' => '获取成功',
  1766. 'data' => $data
  1767. ]);
  1768. } else {
  1769. if (empty($userAuditSetting)) {//判断当前商城用户审核的设置是否存在
  1770. throw new \Exception('系统未配置 无需提交审核');
  1771. }
  1772. if (intval($userAuditSetting->status) === 0) {//判断当前商城用户审核的设置是否开启
  1773. throw new \Exception('系统未配置 无需提交审核');
  1774. }
  1775. //判断当前用户审核记录是否存在或已提交/已通过审核 禁止重复提交
  1776. if (!empty($userAuditLog) && in_array(intval($userAuditLog->status), [UserAuditLog::STATUS_NOT_AGREE, UserAuditLog::STATUS_AGREE])) {
  1777. throw new \Exception('已经提交审核或已通过 请勿重复提交');
  1778. }
  1779. //为空就新增数据库model
  1780. if (empty($userAuditLog)) {
  1781. $userAuditLog = new UserAuditLog();
  1782. $userAuditLog->store_id = $store_id;
  1783. $userAuditLog->user_id = $user->id;
  1784. }
  1785. if (is_wechat_platform()) {
  1786. $userAuditLog->type = UserAuditLog::TYPE_WX;
  1787. }
  1788. if (is_alipay_platform()) {
  1789. $userAuditLog->type = UserAuditLog::TYPE_ALI;
  1790. }
  1791. if (is_app_platform()) {
  1792. $userAuditLog->type = UserAuditLog::TYPE_APP;
  1793. }
  1794. if (is_h5()) {
  1795. $userAuditLog->type = UserAuditLog::TYPE_H5;
  1796. }
  1797. if (!empty($wechat_open_id)) {
  1798. $userAuditLog->wechat_open_id = $wechat_open_id;
  1799. }
  1800. if (!empty($ali_user_id)) {
  1801. $userAuditLog->ali_user_id = $ali_user_id;
  1802. }
  1803. if (!empty($ali_open_id)) {
  1804. $userAuditLog->ali_openId = $ali_open_id;
  1805. }
  1806. $userAuditLog->mobile = $mobile;
  1807. $userAuditLog->avatar = $avatar;
  1808. $userAuditLog->custom_form = json_encode($custom_form, JSON_UNESCAPED_UNICODE);
  1809. $userAuditLog->status = UserAuditLog::STATUS_NOT_AGREE;
  1810. if (!$userAuditLog->save()) {
  1811. return $this->asJson([
  1812. 'code' => 1,
  1813. 'msg' => '保存失败' . json_encode($userAuditLog->errors, JSON_UNESCAPED_UNICODE)
  1814. ]);
  1815. }
  1816. return $this->asJson([
  1817. 'code' => 0,
  1818. 'msg' => '提交成功'
  1819. ]);
  1820. }
  1821. } catch (\Exception $e) {
  1822. return $this->asJson([
  1823. 'code' => 1,
  1824. 'msg' => $e->getMessage()
  1825. ]);
  1826. }
  1827. }
  1828. //月付客户订单记录
  1829. public function actionMoneyMonthLog() {
  1830. $store_id = get_store_id();
  1831. $user_id = get_user_id();
  1832. $month = input_params('month');
  1833. if(empty($month)){
  1834. $month = date('Y-m');
  1835. }
  1836. $begin_time = strtotime($month . '-01 00:00:00');
  1837. $end_time = strtotime(date('Y-m-01', $begin_time + 86400 * 31)) - 1;
  1838. $query = UserPayMonth::find()->where(['store_id' => $store_id, 'user_id' => $user_id]);
  1839. $query->andWhere(['>=', 'created_at', $begin_time]);
  1840. $query->andWhere(['<=', 'created_at', $end_time]);
  1841. $querySum = (clone $query)->andWhere(['type' => 0]);
  1842. $sumMoney = (float)$querySum->sum('money');
  1843. $query->orderBy('id DESC');
  1844. $data = pagination_make($query);
  1845. foreach($data['list'] as &$item){
  1846. $item['day'] = date('n月d日', $item['created_at']);
  1847. }
  1848. return $this->asJson([
  1849. 'q' => $query->createCommand()->getRawSql(),
  1850. 'code' => 0,
  1851. 'msg' => 'ok',
  1852. 'data' => $data,
  1853. 'sumMoney' => $sumMoney,
  1854. 'user_info' => get_user(),
  1855. ]);
  1856. }
  1857. //月付客户历史订单汇总
  1858. public function actionMoneyMonthSum() {
  1859. $store_id = get_store_id();
  1860. $user_id = get_user_id();
  1861. $begin_time = date('Y-m-01 00:00:00', time() - 86400 * 365);
  1862. $query = UserPayMonth::find()->where(['store_id' => $store_id, 'user_id' => $user_id, 'type' => 0]);
  1863. $query->andWhere(['>=', 'created_at', strtotime($begin_time)]);
  1864. $query->groupBy(['pay_year', 'pay_month']);
  1865. $query->orderBy('pay_year DESC');
  1866. $query->addOrderBy(['CONVERT(pay_month, UNSIGNED)' => SORT_DESC]);
  1867. $query->select(['sum(money) AS sum_money', 'FROM_UNIXTIME(created_at,"%Y") AS pay_year', 'FROM_UNIXTIME(created_at,"%c") AS pay_month']);
  1868. $data = $query->asArray()->all();
  1869. return $this->asJson([
  1870. 'code' => 0,
  1871. 'msg' => 'ok',
  1872. 'data' => $data,
  1873. 'user_info' => get_user(),
  1874. ]);
  1875. }
  1876. /**
  1877. * 免单队列
  1878. * @return void
  1879. */
  1880. public function actionFreeQueue()
  1881. {
  1882. $status = get_params('status',0);
  1883. $user_info = User::find()->where(['id' => get_user_id()])->asArray()->one();
  1884. //参与免单人数
  1885. $queue_peopel_num = QueueLog::find()->where(['store_id' => get_store_id()])->andWhere(['!=', 'status', 2])->groupBy('user_id')->count() ?? 0;
  1886. //最新免单返现信息
  1887. $queue_account_new = QueueAccountLog::find()->andWhere(['type' =>0, 'store_id'=>get_store_id()])->orderBy('created_at desc')->asArray()->one();
  1888. if ($queue_account_new) {
  1889. //根据最近返现时间
  1890. $timestamp = $queue_account_new['created_at']; // 示例时间戳
  1891. $now = new \DateTime("now");
  1892. $ago = $now->getTimestamp() - $timestamp;
  1893. $days = intval($ago / (3600 * 24));
  1894. $hours = intval(($ago % (3600 * 24)) / 3600);
  1895. $minutes = intval(($ago % 3600) / 60);
  1896. $result = "";
  1897. if ($days > 0) {
  1898. $result .= $days . "天 ";
  1899. }
  1900. if ($hours > 0) {
  1901. $result .= $hours . "小时 ";
  1902. }
  1903. $result .= $minutes . "分钟";
  1904. $queue_account_new['current_time'] = $result;//获取距离时间
  1905. $new_user_info = User::find()->where(['id' => $queue_account_new['user_id']])->asArray()->one();
  1906. $queue_account_new['nickname'] = mb_substr($new_user_info['nickname'],0,1) .'***';
  1907. $queue_account_new['avatar_url'] = $new_user_info['avatar_url'];
  1908. }else{
  1909. $queue_account_new = [];
  1910. }
  1911. //已返免单金额
  1912. $free_money = $user_info['free_money']??0;
  1913. //累计免单金额
  1914. $total_free_money = $user_info['total_free_money']??0;
  1915. //队列列表
  1916. $query = QueueLog::find()
  1917. ->alias('q')
  1918. ->leftJoin(['o'=> Order::tableName()],'q.order_id = o.id')
  1919. ->leftJoin(['g'=> Goods::tableName()],'g.id = q.goods_id')
  1920. ->leftJoin(['u'=> User::tableName()],'u.id = q.user_id')
  1921. ->where(['q.store_id' => get_store_id()])
  1922. ->andWhere(['!=','q.status',2])
  1923. ->andWhere(['q.user_id' => get_user_id()])
  1924. ->orderBy('q.sort desc');
  1925. if ($status == 0) {
  1926. //
  1927. //排队中,等待发放
  1928. $query->andWhere(['q.status' => 0]);
  1929. }
  1930. if($status== 1){
  1931. //免单中,正在返现
  1932. $query->andWhere(['q.status' => 3]);
  1933. }
  1934. if($status == 2){
  1935. //已完成
  1936. $query->andWhere(['q.status' => 1]);
  1937. }
  1938. $query->select('q.*,o.order_no,o.is_sale,g.cover_pic,g.name as goods_name,u.nickname,g.attr');
  1939. $pagination = pagination_make($query);
  1940. foreach ($pagination['list'] as $key => $value){
  1941. $pagination['list'][$key]['activity'] = \app\models\FreeQueue::findOne($value['queue_id']);
  1942. $attr_info = '';
  1943. foreach (Json::decode($value['attr']) as $k=>$v){
  1944. $attr_info.=$v['attr_group_name'] ?? '规格'.':'.$v['attr_list'][0]['attr_name'].' ';
  1945. }
  1946. $pagination['list'][$key]['attr_info'] = $attr_info;
  1947. $pagination['list'][$key]['goods_price'] =$value['goods_price'] * $value['num'];
  1948. $count = OrderUtil::getQueueSort($value['queue_id'], $value['id'], true);
  1949. $pagination['list'][$key]['total_count'] = $count['total_count'];
  1950. $pagination['list'][$key]['current_sort'] = $count['current_sort'];
  1951. $pagination['list'][$key]['created_at'] = $value['created_at']?date('Y-m-d H:i:s',$value['created_at']):'';
  1952. $pagination['list'][$key]['updated_at'] = $value['updated_at']?date('Y-m-d H:i:s',$value['updated_at']):'';
  1953. }
  1954. return $this->asJson([
  1955. 'code' => 0,
  1956. 'msg' => '修改成功',
  1957. 'data'=>[
  1958. 'queue_account'=>$queue_account_new,
  1959. 'queue_peopel_num'=>$queue_peopel_num,//参与免单人数
  1960. 'free_money'=>$free_money,//免单金额
  1961. 'total_free_money'=>$total_free_money,//累计免单金额
  1962. 'data' => $pagination['list'],
  1963. 'pageNo' => $pagination['pageNo'],
  1964. 'totalCount' => $pagination['totalCount'],
  1965. ]
  1966. ]);
  1967. }
  1968. /**
  1969. * 免单返现明细
  1970. * @return \yii\web\Response
  1971. */
  1972. public function actionQueueAccountLog()
  1973. {
  1974. $status = get_params('status',-1);
  1975. //队列列表
  1976. $query = QueueAccountLog::find()
  1977. ->where(['store_id' => get_store_id()])
  1978. ->andWhere(['user_id'=>get_user_id()])
  1979. ->orderBy('created_at desc');
  1980. if($status == 1){
  1981. //已收入
  1982. $query->andWhere(['type' => 0]);
  1983. }
  1984. if($status== 2){
  1985. //已支出
  1986. $query->andWhere(['type' => 1]);
  1987. }
  1988. $pagination = pagination_make($query);
  1989. if (count($pagination['list']) > 0){
  1990. foreach ($pagination['list'] as $key => $value){
  1991. //查询订单号
  1992. if ($value['type'] == 0){
  1993. $pagination['list'][$key]['order_no'] = Order::find()->where(['id' => $value['order_id']])->one()['order_no'];
  1994. }
  1995. if ($value['type'] == 1){
  1996. $pagination['list'][$key]['order_no'] = Cash::find()->where(['id' => $value['order_id']])->one()['order_no'];
  1997. }
  1998. $pagination['list'][$key]['created_at'] = $value['created_at']?date('Y-m-d H:i:s',$value['created_at']):'';
  1999. }
  2000. }
  2001. return $this->asJson([
  2002. 'code' => 0,
  2003. 'msg' => '修改成功',
  2004. 'data'=>[
  2005. 'data' => $pagination['list'],
  2006. 'pageNo' => $pagination['pageNo'],
  2007. 'totalCount' => $pagination['totalCount'],
  2008. ]
  2009. ]);
  2010. }
  2011. public function actionQueueToIntegral()
  2012. {
  2013. $store_id = get_store_id();
  2014. $queue_id = \post_params('queue_log_id');
  2015. if (!$queue_id) {
  2016. return $this->asJson([
  2017. 'code' => 1,
  2018. 'msg' => '参数错误',
  2019. ]);
  2020. }
  2021. $integral_bili = \app\models\Option::get('integral', $store_id, 'store', 0)['value'];
  2022. if ($integral_bili == 0) {
  2023. return $this->asJson([
  2024. 'code' => 1,
  2025. 'msg' => '暂未设置兑换比例',
  2026. ]);
  2027. }
  2028. $queue_log = QueueLog::find()->where(['id' => $queue_id])->one();
  2029. if ($queue_log->is_meet == 1) {
  2030. return $this->asJson([
  2031. 'code' => 1,
  2032. 'msg' => '该队列已返现',
  2033. ]);
  2034. }
  2035. if ($queue_log->status == 2) {
  2036. return $this->asJson([
  2037. 'code' => 1,
  2038. 'msg' => '该队列已取消',
  2039. ]);
  2040. }
  2041. if ($queue_log->status == 1) {
  2042. return $this->asJson([
  2043. 'code' => 1,
  2044. 'msg' => '该队列已完成',
  2045. ]);
  2046. }
  2047. // if ($queue_log->status == 3) {
  2048. // return $this->asJson([
  2049. // 'code' => 1,
  2050. // 'msg' => '该队列已经在进行中了,无法兑换',
  2051. // ]);
  2052. // }
  2053. $user = User::findOne($queue_log->user_id);
  2054. if (!$user) {
  2055. return $this->asJson([
  2056. 'code' => 1,
  2057. 'msg' => '用户不存在',
  2058. ]);
  2059. }
  2060. $total_money = $queue_log->total_money - $queue_log->returned_money;
  2061. $integral = \round($total_money * $integral_bili, 2);
  2062. $before = $user->integral;
  2063. $user->integral += $integral;
  2064. $user->total_integral += $integral;
  2065. $user->save();
  2066. $queue_log->is_meet = 1;
  2067. $queue_log->status = 1;
  2068. $queue_log->integral = $integral;
  2069. $queue_log->save();
  2070. $log = new AccountLog();
  2071. $log->store_id = $store_id;
  2072. $log->user_id = $queue_log->user_id;
  2073. $log->type = AccountLog::TYPE_INTEGRAL;
  2074. $log->log_type = AccountLog::LOG_TYPE_INCOME;
  2075. $log->amount = $integral;
  2076. $log->desc = '免单队列转积分';
  2077. $log->before = $before;
  2078. $log->after = $user->integral;
  2079. $log->operator = '';
  2080. $log->operator_id = 0;
  2081. $log->operator_type = AccountLog::TYPE_OPERATOR_NORMAL;
  2082. $log->created_at = time();
  2083. $log->order_id = $queue_log->order_id;
  2084. $log->order_type = AccountLog::TYPE_PLATFORM_ORDER;
  2085. $log->save();
  2086. return $this->asJson([
  2087. 'code' => 0,
  2088. 'msg' => '兑换成功',
  2089. ]);
  2090. }
  2091. /**
  2092. * 灵工-收款人进件提交
  2093. * @return \yii\web\Response
  2094. */
  2095. public function actionLgSubmit() {
  2096. $form = new LgForm();
  2097. $form->attributes = post_params();
  2098. /* begin 2025/07/08 09:21:37 灵工进件时如果未匹配到银行编码填默认的9999 WPing丶 */
  2099. if(!post_params('bank_code')) {
  2100. $form->bank_code = '9999';
  2101. }
  2102. /* end */
  2103. $form->store_id = get_store_id();
  2104. $form->user_id = get_saas_user_id();
  2105. return $this->asJson($form->save());
  2106. }
  2107. /**
  2108. * 灵工-收款人进件状态
  2109. * @return \yii\web\Response
  2110. */
  2111. public function actionLgAuditStatus() {
  2112. $saas_user_id = get_saas_user_id();
  2113. $lg = Lg::find()->where(['user_id' => $saas_user_id, 'store_id' => get_store_id(), 'is_delete' => 0])
  2114. ->orderBy('id desc')
  2115. ->limit(1)
  2116. ->one();
  2117. if (!$lg) {
  2118. return $this->asJson([
  2119. 'code' => 0,
  2120. 'msg' => '进件记录不存在',
  2121. 'data' => ['status' => -1]
  2122. ]);
  2123. }
  2124. return $this->asJson([
  2125. 'code' => 0,
  2126. 'data' => ['status' => $lg->status,'id'=> $lg->id,'refuse_desc'=>$lg->refuse_desc]
  2127. ]);
  2128. }
  2129. /**
  2130. * 灵工-获取收款人银行信息
  2131. * @return \yii\web\Response
  2132. */
  2133. public function actionLgBank() {
  2134. $lgApi = new LgApi(get_store_id());
  2135. //需要通过三方查询收款银行信息编码
  2136. $data = $lgApi->FlexibleBanks();
  2137. return $this->asJson([
  2138. 'code' => 0,
  2139. 'data' => $data
  2140. ]);
  2141. }
  2142. /**
  2143. * 灵工-收款人进件状态查询
  2144. * @return \yii\web\Response
  2145. */
  2146. public function lgQuery($certCard,$user_id) {
  2147. $post_data = [];
  2148. $post_data['certCard'] = $certCard;
  2149. $lgApi = new LgApi(get_store_id());
  2150. $res = $lgApi->FlexibleQuery($post_data);
  2151. $lg = Lg::find()->where(['cert_card' => $certCard, 'store_id' => get_store_id(), 'is_delete' => 0,'user_id'=>$user_id])->one();
  2152. if (!$lg){
  2153. \Yii::error('灵工-收款人进件状态查询-进件记录不存在'.$certCard);
  2154. return false;
  2155. }
  2156. //审核中
  2157. if ($res['status'] == 100 || $res['status'] == 200){
  2158. $lg->status = 0;
  2159. $lg->updated_at = time();
  2160. $lg->save();
  2161. }
  2162. //成功
  2163. if ($res['status'] == 300){
  2164. $lg->status = 1;
  2165. $lg->updated_at = time();
  2166. $lg->save();
  2167. }
  2168. //失败
  2169. if ($res['status'] == 999){
  2170. $lg->status = 2;
  2171. $lg->updated_at = time();
  2172. $lg->refuse_desc = $res['msg'];
  2173. $lg->save();
  2174. }
  2175. }
  2176. /**
  2177. * 灵工-收款人进件信息
  2178. * @return \yii\web\Response
  2179. */
  2180. public function actionLgInfo() {
  2181. $saas_user_id = get_saas_user_id();
  2182. $lg = Lg::find()->where(['user_id' => $saas_user_id, 'store_id' => get_store_id(), 'is_delete' => 0])
  2183. ->asArray()
  2184. ->one();
  2185. if (!$lg) {
  2186. return $this->asJson([
  2187. 'code' => 1,
  2188. 'msg' => '进件记录不存在',
  2189. ]);
  2190. }
  2191. $lg['id_card_start'] = $lg['id_card_start']?date('Y-m-d',$lg['id_card_start']):'';
  2192. $lg['id_card_end'] = $lg['id_card_end']?date('Y-m-d',$lg['id_card_end']):'';
  2193. return $this->asJson([
  2194. 'code' => 0,
  2195. 'data' => [
  2196. 'apply' => $lg
  2197. ],
  2198. ]);
  2199. }
  2200. // 获取用户坐标
  2201. public function actionGetGeo()
  2202. {
  2203. try {
  2204. $access_token = \input_params('access_token');
  2205. if (!$access_token) {
  2206. throw new \Exception('access_token不能为空');
  2207. }
  2208. $saasUser = SaasUser::findOne(['access_token' => $access_token, 'is_delete' => 0]);
  2209. if (!$saasUser) {
  2210. throw new \Exception('access_token无效');
  2211. }
  2212. $addressModel = Address::findOne(['user_id' => $saasUser->id, 'is_default' => 1, 'is_delete' => 0]);
  2213. if (!$addressModel) {
  2214. throw new \Exception('地址不存在');
  2215. }
  2216. $address = $addressModel->province . $addressModel->city . $addressModel->district . $addressModel->town . $addressModel->detail;
  2217. if ($addressModel->latitude && $addressModel->longitude) {
  2218. return $this->asJson([
  2219. 'code' => 0,
  2220. 'data' => [
  2221. 'latitude' => $addressModel->latitude,
  2222. 'longitude' => $addressModel->longitude,
  2223. 'address' => $address,
  2224. ],
  2225. ]);
  2226. }
  2227. $appcode = Option::get(OptionSetting::MAP_APP_CODE, get_store_id(), 'app', '')['value'];
  2228. if (empty($appcode)) {
  2229. throw new \Exception('地图服务未配置');
  2230. }
  2231. $res = \app\utils\AddressGeo::AliCloud($address, $appcode);
  2232. return $this->asJson([
  2233. 'code' => 0,
  2234. 'data' => [
  2235. 'latitude' => $res['lat'],
  2236. 'longitude' => $res['lng'],
  2237. 'address' => $address,
  2238. ],
  2239. ]);
  2240. } catch (\Throwable $e) {
  2241. return $this->asJson([
  2242. 'code' => 0,
  2243. 'data' => [
  2244. 'latitude' => 0,
  2245. 'longitude' => 0,
  2246. 'address' => '',
  2247. ],
  2248. ]);
  2249. }
  2250. }
  2251. /**
  2252. * 获取用户成长值记录
  2253. * @return \yii\web\Response
  2254. * @author: hankaige
  2255. * @Time: 2025/4/22 09:08
  2256. */
  2257. public function actionGetGrowthLog(){
  2258. $user = get_user();
  2259. $storeId = get_store_id();
  2260. $query = UserGrowthValueLog::find()->where(['user_id' => $user->id,'store_id' => $storeId]);
  2261. $list = pagination_make($query,true,'created_at DESC');
  2262. foreach($list['list'] as &$item){
  2263. $item['created_at'] = date('Y-m-d H:i:s',$item['created_at']);
  2264. $item['from_text'] = UserGrowthValueLog::TYPE_MAP[$item['from']];
  2265. }
  2266. $list['growth_value'] = $user->growth_value;
  2267. return $this->asJson([
  2268. 'code' => 0,
  2269. 'msg' => 'ok',
  2270. 'data' => $list,
  2271. ]);
  2272. }
  2273. }