ReportController.php 85 KB

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