Member.php 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  1. <?php
  2. namespace app\common\models;
  3. use app\backend\models\BackendModel;
  4. use app\backend\modules\member\models\MemberUnique;
  5. use app\common\events\member\BecomeAgent;
  6. use app\common\events\member\PluginCreateRelationEvent;
  7. use app\common\exceptions\AppException;
  8. use app\common\exceptions\MemberNotLoginException;
  9. use app\common\facades\Setting;
  10. use app\common\models\member\MemberChildren;
  11. use app\common\models\member\MemberDel;
  12. use app\common\models\member\MemberParent;
  13. use app\common\models\point\PointBalanceSet;
  14. use app\common\observers\McMemberObserver;
  15. use app\common\services\PluginManager;
  16. use app\common\modules\memberCart\MemberCartCollection;
  17. use app\common\services\popularize\PortType;
  18. use app\framework\Database\Eloquent\Collection;
  19. use app\frontend\modules\member\models\MemberModel;
  20. use app\frontend\modules\member\models\MemberWechatModel;
  21. use app\frontend\repositories\MemberAddressRepository;
  22. use Carbon\Carbon;
  23. use Yunshop\AggregationCps\models\AggregationCpsOrderModel;
  24. use Yunshop\AggregationCps\models\MemberAggregationAppModel;
  25. use Yunshop\AreaDividend\models\AreaDividendAgent;
  26. use Yunshop\Commission\models\Agents;
  27. use Yunshop\Gold\frontend\services\MemberCenterService;
  28. use Yunshop\Love\Common\Models\MemberLove;
  29. use Yunshop\Love\Common\Services\SetService;
  30. use Yunshop\MemberTags\Common\models\MemberTagsRelationModel;
  31. use Yunshop\Merchant\common\models\Merchant;
  32. use Yunshop\Micro\common\models\MicroShop;
  33. use Yunshop\Micro\common\services\MicroShop\GetButtonService;
  34. use Yunshop\StoreCashier\common\models\Store;
  35. use Yunshop\Supplier\admin\models\Supplier;
  36. use Yunshop\Supplier\common\services\VerifyButton;
  37. use Yunshop\TeamDividend\models\TeamDividendAgencyModel;
  38. use app\common\models\member\MemberInvitationCodeLog;
  39. use Yunshop\WechatCustomers\common\models\MemberCustomer;
  40. /**
  41. * Created by PhpStorm.
  42. * Author: 芸众商城 www.yunzshop.com
  43. * Date: 21/02/2017
  44. * Time: 12:58
  45. */
  46. /**
  47. * Class Member
  48. * @package app\common\models
  49. * @property int uid
  50. * @property int uniacid
  51. * @property string mobile
  52. * @property string email
  53. * @property string password
  54. * @property string salt
  55. * @property int groupid
  56. * @property float credit1
  57. * @property float credit2
  58. * @property float credit3
  59. * @property float credit4
  60. * @property float credit5
  61. * @property float credit6
  62. * @property Carbon createtime
  63. * @property string realname
  64. * @property string nickname
  65. * @property string avatar
  66. * @property string qq
  67. * @property int vip
  68. * @property int gender
  69. * @property int birthyear
  70. * @property int birthmonth
  71. * @property int birthday
  72. * @property string constellation
  73. * @property string zodiac
  74. * @property string telephone
  75. * @property string idcard
  76. * @property string studentid
  77. * @property string grade
  78. * @property string address
  79. * @property string zipcode
  80. * @property string nationality
  81. * @property string resideprovince
  82. * @property string residecity
  83. * @property string residedist
  84. * @property string graduateschool
  85. * @property string company
  86. * @property string education
  87. * @property string occupation
  88. * @property string position
  89. * @property string revenue
  90. * @property string affectivestatus
  91. * @property string lookingfor
  92. * @property string bloodtype
  93. * @property string height
  94. * @property string weight
  95. * @property string alipay
  96. * @property string msn
  97. * @property string taobao
  98. * @property string site
  99. * @property string bio
  100. * @property string interest
  101. * @property string pay_password
  102. * @property Collection memberCarts
  103. * @property McMappingFans hasOneFans
  104. * @property MemberMiniAppModel hasOneMiniApp
  105. * @property \app\backend\modules\member\models\MemberShopInfo yzMember
  106. * @property MemberDel hasOneDel
  107. *
  108. * @method static self withoutDeleted()
  109. * @method static self searchYzMember($search = [])
  110. */
  111. class Member extends BackendModel
  112. {
  113. static $current;
  114. protected $connection = 'mysql';
  115. public $table = 'mc_members';
  116. public $timestamps = false;
  117. protected $guarded = ['credit1', 'credit2', 'credit3', 'credit4', 'credit5'];
  118. protected $fillable = ['uniacid', 'mobile', 'groupid', 'createtime', 'nickname', 'avatar', 'gender', 'salt', 'password'];
  119. protected $attributes = ['bio' => '', 'resideprovince' => '', 'residecity' => '', 'nationality' => '', 'interest' => '', 'mobile' => '', 'email' => '', 'credit1' => 0, 'credit2' => 0, 'credit3' => 0, 'credit4' => 0, 'credit5' => 0, 'credit6' => 0, 'realname' => '', 'qq' => '', 'vip' => 0, 'birthyear' => 0, 'birthmonth' => 0, 'birthday' => 0, 'constellation' => '', 'zodiac' => '', 'telephone' => '', 'idcard' => '', 'studentid' => '', 'grade' => '', 'address' => '', 'zipcode' => '', 'residedist' => '', 'graduateschool' => '', 'company' => '', 'education' => '', 'occupation' => '', 'position' => '', 'revenue' => '', 'affectivestatus' => '', 'lookingfor' => '', 'bloodtype' => '', 'height' => '', 'weight' => '', 'alipay' => '', 'msn' => '', 'taobao' => '', 'site' => ''];
  120. const INVALID_OPENID = 0;
  121. protected $search_fields = ['uid', 'nickname', 'realname', 'mobile'];
  122. protected $primaryKey = 'uid';
  123. protected $appends = ['avatar_image', 'username'];
  124. protected $hidden = ['password', 'salt'];
  125. public function bankCard()
  126. {
  127. return $this->hasOne('app\common\models\member\BankCard', 'member_id', 'uid');
  128. }
  129. /**
  130. * @return \app\frontend\models\Member
  131. * @throws AppException
  132. */
  133. public static function current()
  134. {
  135. if (!isset(static::$current)) {
  136. static::$current = self::find(\YunShop::app()->getMemberId());
  137. if (!static::$current) {
  138. return new Member();
  139. }
  140. }
  141. return static::$current;
  142. }
  143. public static function boot()
  144. {
  145. parent::boot();
  146. self::observe(new McMemberObserver());
  147. }
  148. public function pointLove()
  149. {
  150. return $this->hasOne('app\common\models\finance\PointLoveSet', 'member_id', 'uid');
  151. }
  152. //关联会员删除表 yz_member_del_log
  153. public function hasOneDel()
  154. {
  155. return $this->hasOne('app\common\models\member\MemberDel', 'member_id', 'uid');
  156. }
  157. public function defaultAddress()
  158. {
  159. return $this->hasOne(app(MemberAddressRepository::class)->model(), 'uid', 'uid')->where('isdefault', 1);
  160. }
  161. /**
  162. * 主从表1:1
  163. *
  164. * @return mixed
  165. */
  166. public function yzMember()
  167. {
  168. return $this->hasOne('app\backend\modules\member\models\MemberShopInfo', 'member_id', 'uid');
  169. }
  170. /**
  171. * 会员-粉丝1:1关系
  172. *
  173. * @return mixed
  174. */
  175. //公众号
  176. public function hasOneFans()
  177. {
  178. return $this->hasOne('app\common\models\McMappingFans', 'uid', 'uid');
  179. }
  180. //小程序
  181. public function hasOneMiniApp()
  182. {
  183. return $this->hasOne(MemberMiniAppModel::class, 'member_id', 'uid');
  184. }
  185. //开放平台
  186. public function hasOneUnique()
  187. {
  188. return $this->hasOne(MemberUnique::class, 'member_id', 'uid');
  189. }
  190. //app
  191. public function hasOneWechat()
  192. {
  193. return $this->hasOne(\app\common\models\MemberWechatModel::class, 'member_id', 'uid');
  194. }
  195. //抖音
  196. public function hasOneDouyin()
  197. {
  198. return $this->hasOne(MemberDouyinModel::class, 'member_id', 'uid');
  199. }
  200. //支付宝
  201. public function hasOneAlipay()
  202. {
  203. return $this->hasOne(MemberAlipay::class, 'member_id', 'uid');
  204. }
  205. //会员标签
  206. public function hasManyTag()
  207. {
  208. return $this->hasMany(MemberTagsRelationModel::class, 'member_id', 'uid');
  209. }
  210. //企业微信客户
  211. public function hasOneCustomers()
  212. {
  213. return $this->hasOne(MemberCustomer::class, 'uid', 'uid');
  214. }
  215. /**
  216. * 会员-订单1:1关系 todo 会员和订单不是一对多关系吗?
  217. *
  218. * @return mixed
  219. */
  220. public function hasOneOrder()
  221. {
  222. return $this->hasOne('app\common\models\Order', 'uid', 'uid');
  223. }
  224. public function hasManyOrder()
  225. {
  226. return $this->hasMany('app\common\models\Order', 'uid', 'uid');
  227. }
  228. /**
  229. * 会员-会员优惠券1:多关系
  230. *
  231. * @return mixed
  232. */
  233. public function hasManyMemberCoupon()
  234. {
  235. return $this->hasOne(MemberCoupon::class, 'uid', 'uid');
  236. }
  237. /**
  238. * 公众号会员
  239. *
  240. * @return mixed
  241. */
  242. public function getMemberId($memberIds)
  243. {
  244. return self::select(['uid'])
  245. ->uniacid()
  246. ->whereIn('uid', $memberIds)->get()->map(function ($value) {
  247. return $value;
  248. })->toArray();
  249. }
  250. /**
  251. * 角色
  252. *
  253. * 会员-分销商
  254. *
  255. * @return \Illuminate\Database\Eloquent\Relations\HasOne
  256. */
  257. public function hasOneAgent()
  258. {
  259. return $this->hasOne(Agents::class, 'member_id', 'uid');
  260. }
  261. /**
  262. * 角色
  263. *
  264. * 会员-经销商
  265. *
  266. * @return \Illuminate\Database\Eloquent\Relations\HasOne
  267. */
  268. public function hasOneTeamDividend()
  269. {
  270. return $this->hasOne(TeamDividendAgencyModel::class, 'uid', 'uid');
  271. }
  272. /**
  273. * 角色
  274. *
  275. * 会员-区域代理
  276. *
  277. * @return \Illuminate\Database\Eloquent\Relations\HasOne
  278. */
  279. public function hasOneAreaDividend()
  280. {
  281. return $this->hasOne(AreaDividendAgent::class, 'member_id', 'uid');
  282. }
  283. /**
  284. * 角色
  285. *
  286. * 会员-招商员
  287. *
  288. * @return \Illuminate\Database\Eloquent\Relations\HasOne
  289. */
  290. public function hasOneMerchant()
  291. {
  292. return $this->hasOne(Merchant::class, 'member_id', 'uid');
  293. }
  294. /**
  295. * 角色
  296. *
  297. * 会员-招商中心
  298. *
  299. * @return \Illuminate\Database\Eloquent\Relations\HasOne
  300. */
  301. public function hasOneMerchantCenter()
  302. {
  303. return $this->hasOne(Merchant::class, 'member_id', 'uid');
  304. }
  305. /**
  306. * 角色
  307. *
  308. * 会员-微店店主
  309. *
  310. * @return \Illuminate\Database\Eloquent\Relations\HasOne
  311. */
  312. public function hasOneMicro()
  313. {
  314. return $this->hasOne(MicroShop::class, 'member_id', 'uid');
  315. }
  316. /**
  317. * 角色
  318. *
  319. * 会员-供应商
  320. *
  321. * @return \Illuminate\Database\Eloquent\Relations\HasOne
  322. */
  323. public function hasOneSupplier()
  324. {
  325. return $this->hasOne(Supplier::class, 'member_id', 'uid');
  326. }
  327. /**
  328. * 子会员
  329. *
  330. * 会员-子会员
  331. *
  332. * @return \Illuminate\Database\Eloquent\Relations\HasOne
  333. */
  334. public function hasOneMemberChildren()
  335. {
  336. return $this->hasOne(MemberChildren::class, 'member_id', 'uid');
  337. }
  338. public function hasOneMemberUnique()
  339. {
  340. return $this->hasOne(MemberUnique::class, 'member_id', 'uid');
  341. }
  342. public function hasOneMemberLove()
  343. {
  344. return $this->hasOne(MemberLove::class, 'member_id', 'uid');
  345. }
  346. public function scopeOfUid($query, $uid)
  347. {
  348. return $query->where('uid', $uid);
  349. }
  350. public function levelName()
  351. {
  352. return $this->yzMember->level->level_name ?? Setting::get('shop.member.level_name') ?? "普通会员";
  353. }
  354. /**
  355. * @param static $query
  356. */
  357. public function scopeWithoutDeleted($query)
  358. {
  359. $query->whereHas('yzMember', function ($query) {
  360. $query->whereNull('deleted_at');
  361. })->leftJoin('yz_member_del_log', 'mc_members.uid', '=', 'yz_member_del_log.member_id')->whereNull('yz_member_del_log.member_id');
  362. }
  363. /**
  364. * @param static $query
  365. * @param array $search
  366. */
  367. public function scopeSearch($query, $search)
  368. {
  369. $query->searchYzMember($search);
  370. if ($search['member']) $query->searchLike($search['member']);
  371. if ($search['member_id']) $query->where('uid', $search['member_id']);
  372. if ($search['min_credit2']) $query->where('credit2', '>=', $search['min_credit2']);
  373. if ($search['max_credit2']) $query->where('credit2', '<=', $search['max_credit2']);
  374. //todo 移除一下代码,规范使用,如果模糊查询 会员ID、昵称、姓名、手机号使用 $search['member']
  375. if ($search['realname']) $query->searchLike($search['realname']);
  376. }
  377. /**
  378. * todo 移除 $search['member_level'] || $search['member_group']
  379. *
  380. * @param static $query
  381. * @param array $search
  382. */
  383. public function scopeSearchYzMember($query, $search)
  384. {
  385. if ($search['level_id'] || $search['group_id'] || $search['member_level'] || $search['member_group']) {
  386. $query->whereHas('yzMember', function ($query) use ($search) {
  387. /**
  388. * @var MemberShopInfo $query
  389. */
  390. $query->search($search);
  391. });
  392. }
  393. }
  394. /**
  395. * 获取用户信息
  396. *
  397. * @param $member_id
  398. * @return mixed
  399. */
  400. public static function getUserInfos($member_id)
  401. {
  402. return self::select(['*'])
  403. ->uniacid()
  404. ->where('uid', $member_id)
  405. ->with([
  406. 'yzMember' => function ($query) {
  407. return $query->select(['*'])->where('is_black', 0)
  408. ->with([
  409. 'group' => function ($query1) {
  410. return $query1->select(['id', 'group_name']);
  411. },
  412. 'level' => function ($query2) {
  413. return $query2->select(['id', 'level_name']);
  414. },
  415. 'agent' => function ($query3) {
  416. return $query3->select(['uid', 'avatar', 'nickname']);
  417. }
  418. ]);
  419. },
  420. 'hasOneFans' => function ($query4) {
  421. return $query4->select(['uid', 'openid', 'follow as followed']);
  422. },
  423. 'hasOneOrder' => function ($query5) {
  424. return $query5->selectRaw('uid, count(uid) as total, sum(price) as sum')
  425. ->uniacid()
  426. ->where('status', 3)
  427. ->groupBy('uid');
  428. }
  429. ]);
  430. }
  431. /**
  432. * 获取该公众号下所有用户的 member ID
  433. *
  434. * @return mixed
  435. */
  436. public static function getMembersId()
  437. {
  438. return static::uniacid()
  439. ->select(['uid'])
  440. ->get();
  441. }
  442. /**
  443. * 通过id获取用户信息
  444. *
  445. * @param $member_id
  446. * @return mixed
  447. */
  448. public static function getMemberById($member_id)
  449. {
  450. return self::uniacid()
  451. ->where('uid', $member_id)
  452. ->first();
  453. }
  454. public static function getMemberByUid($member_id)
  455. {
  456. return self::uniacid()
  457. ->where('uid', $member_id);
  458. }
  459. /**
  460. * 添加评论默认名称
  461. * @return mixed
  462. */
  463. public static function getRandNickName()
  464. {
  465. return self::select('nickname')
  466. ->whereNotNull('nickname')
  467. ->inRandomOrder()
  468. ->first();
  469. }
  470. /**
  471. * 昵称手机号隐藏
  472. * @return array|mixed|string|string[]|null
  473. */
  474. public function getNicknameAttribute()
  475. {
  476. $preg = '/^1(3\d|4[5-9]|5[0-35-9]|6[567]|7[0-8]|8\d|9[0-35-9])\d{8}$/';
  477. if (preg_match($preg,$this->getAttributeFromArray('nickname'))) {
  478. return substr_replace($this->getAttributeFromArray('nickname'),'*******',2,7);
  479. }
  480. return $this->getAttributeFromArray('nickname');
  481. }
  482. /**
  483. * 会员头像格式化
  484. * @return array|mixed|string|string[]|null
  485. */
  486. public function getAvatarAttribute()
  487. {
  488. return $this->getAttributeFromArray('avatar') ? yz_tomedia($this->getAttributeFromArray('avatar')) : yz_tomedia(\Setting::get('shop.member.headimg'));
  489. }
  490. /**
  491. * 判断是否有设置过头像
  492. * @return array|mixed|string|string[]|null
  493. */
  494. public function getHasAvatarAttribute()
  495. {
  496. return $this->getAttributeFromArray('avatar') && $this->getAttributeFromArray('nickname') ? 1 : 0;
  497. }
  498. /**
  499. * 添加评论默认头像
  500. * @return mixed
  501. */
  502. public static function getRandAvatar()
  503. {
  504. return self::select('avatar')
  505. ->whereNotNull('avatar')
  506. ->inRandomOrder()
  507. ->first();
  508. }
  509. public static function getOpenId($member_id)
  510. {
  511. $data = self::getUserInfos($member_id)->first();
  512. if ($data) {
  513. $info = $data->toArray();
  514. if (!empty($info['has_one_fans'])) {
  515. return $info['has_one_fans']['openid'];
  516. } else {
  517. return self::INVALID_OPENID;
  518. }
  519. }
  520. }
  521. /**
  522. * 触发会员成为下线事件
  523. *
  524. * @param $member_id
  525. */
  526. public static function chkAgent($member_id, $mid, $mark = '', $mark_id = '')
  527. {
  528. $model = MemberShopInfo::getMemberShopInfo($member_id);
  529. if (1 != $model->inviter && 2 != $model->status) {
  530. $relation = new MemberRelation();
  531. $relation->becomeChildAgent($mid, $model);
  532. }
  533. if ($mark_id && $mark) {
  534. event(new PluginCreateRelationEvent($mid, $model, $mark, $mark_id));
  535. }
  536. }
  537. /**
  538. * 定义字段名
  539. *
  540. * @return array
  541. */
  542. public function atributeNames()
  543. {
  544. return [
  545. 'mobile' => '绑定手机号',
  546. 'realname' => '真实姓名',
  547. //'avatar' => '头像',
  548. 'telephone' => '联系手机号',
  549. ];
  550. }
  551. /**
  552. * 字段规则
  553. *
  554. * @return array
  555. */
  556. public function rules()
  557. {
  558. $rule = [
  559. // 'mobile' => 'required|numeric',
  560. // 'realname' => 'required|between:2,10',
  561. //'avatar' => 'required',
  562. 'telephone' => 'regex:/^1\d{10}$/',
  563. ];
  564. if (\Setting::get('shop.form')['base']['name'] == 1) {
  565. $rule['realname'] = 'required|between:2,10';
  566. }
  567. return $rule;
  568. }
  569. /**
  570. * 生成分销关系链
  571. *
  572. * @param $member_id
  573. */
  574. public static function createRealtion($member_id, $upperMemberId = NULL)
  575. {
  576. $model = MemberShopInfo::getMemberShopInfo($member_id);
  577. $code_mid = self::getMemberIdForInviteCode();
  578. if (!is_null($code_mid)) {
  579. //邀请码关系链
  580. $code_model = new MemberInvitationCodeLog();
  581. $code_model->uniacid = \YunShop::app()->uniacid;
  582. $code_model->invitation_code = trim(\YunShop::request()->invite_code);
  583. $code_model->member_id = $member_id; //使用者id
  584. $code_model->mid = $code_mid; //邀请人id
  585. $code_model->save();
  586. file_put_contents(storage_path("logs/" . date('Y-m-d') . "_invitecode.log"), print_r($member_id . '-' . \YunShop::request()->invite_code . '-' . $code_mid . '-reg' . PHP_EOL, 1), FILE_APPEND);
  587. }
  588. $mid = !is_null($code_mid) ? $code_mid : self::getMid();
  589. $mid = !is_null($upperMemberId) ? $upperMemberId : $mid;
  590. \Log::debug('--------member_id---------', $member_id);
  591. \Log::debug('--------upper_member_id---------', $upperMemberId);
  592. \Log::debug('--------mid---------', self::getMid());
  593. \Log::debug('--------code_mid---------', $code_mid);
  594. event(new BecomeAgent($mid, $model));
  595. }
  596. public static function getMid()
  597. {
  598. $mid = \YunShop::request()->mid;
  599. return ($mid && ($mid != 'null' || $mid != 'undefined')) ? (int)$mid : 0;
  600. }
  601. /**
  602. * 申请插件
  603. *
  604. * @param array $data
  605. * @return array
  606. */
  607. public static function addPlugins(&$data = [])
  608. {
  609. $plugin_class = app('plugins');
  610. //供应商
  611. if ($plugin_class->isEnabled('supplier')) {
  612. $data['supplier'] = VerifyButton::button();
  613. } else {
  614. $data['supplier'] = '';
  615. }
  616. //微店
  617. if ($plugin_class->isEnabled('micro')) {
  618. $micro_set = \Setting::get('plugin.micro');
  619. if ($micro_set['is_open_miceo'] == 0) {
  620. $data['micro'] = '';
  621. } else {
  622. $data['micro'] = GetButtonService::
  623. verify(\YunShop::app()->getMemberId());
  624. }
  625. } else {
  626. $data['micro'] = '';
  627. }
  628. if ($plugin_class->isEnabled('gold')) {
  629. $data['gold'] = MemberCenterService::button(\YunShop::app()->getMemberId());
  630. } else {
  631. $data['gold'] = '';
  632. }
  633. //爱心值
  634. if ($plugin_class->isEnabled('love')) {
  635. $data['love'] = [
  636. 'status' => true,
  637. 'love_name' => SetService::getLoveName(),
  638. ];
  639. } else {
  640. $data['love'] = [
  641. 'status' => false,
  642. 'love_name' => '爱心值',
  643. ];
  644. }
  645. if ($plugin_class->isEnabled('froze')) {
  646. $data['froze'] = [
  647. 'status' => true,
  648. 'froze_name' => \Yunshop\Froze\Common\Services\SetService::getFrozeName(),
  649. ];
  650. } else {
  651. $data['froze'] = [
  652. 'status' => false,
  653. 'froze_name' => '冻结币',
  654. ];
  655. }
  656. if ($plugin_class->isEnabled('coin')) {
  657. $data['coin'] = [
  658. 'status' => true,
  659. 'coin_name' => \Yunshop\Coin\Common\Services\SetService::getCoinName(),
  660. ];
  661. } else {
  662. $data['coin'] = [
  663. 'status' => false,
  664. 'coin_name' => '华侨币',
  665. ];
  666. }
  667. if ($plugin_class->isEnabled('store-cashier')) {
  668. $store = Store::getStoreByUid(\YunShop::app()->getMemberId())->first();
  669. if ($store && $store->hasOneCashier->hasOneCashierGoods->is_open == 1) {
  670. $data['cashier'] = [
  671. 'button_name' => '收银台',
  672. 'api' => 'plugin.store-cashier.frontend.cashier.center.index'
  673. ];
  674. }
  675. } else {
  676. $data['cashier'] = '';
  677. }
  678. if ($plugin_class->isEnabled('elive')) {
  679. $data['elive'] = [
  680. 'button_name' => '生活缴费',
  681. 'status' => true
  682. ];
  683. } else {
  684. $data['elive'] = ['status' => false];
  685. }
  686. if ($plugin_class->isEnabled('sign')) {
  687. $data['sign'] = [
  688. 'status' => true,
  689. 'plugin_name' => trans('Yunshop\Sign::sign.plugin_name'),
  690. ];
  691. } else {
  692. $data['sign'] = [
  693. 'status' => false,
  694. 'plugin_name' => '签到',
  695. ];
  696. }
  697. //快递单插件开启
  698. if ($plugin_class->isEnabled('courier')) {
  699. $status = \Setting::get('courier.courier.radio') ? true : false;
  700. $data['courier'] = [
  701. 'button_name' => '快递',
  702. 'status' => $status
  703. ];
  704. } else {
  705. $data['courier'] = [
  706. 'button_name' => '快递',
  707. 'status' => false
  708. ];
  709. }
  710. //帮助中心插件开启控制
  711. if ($plugin_class->isEnabled('help-center')) {
  712. $status = \Setting::get('help-center.status') ? true : false;
  713. $data['help_center'] = [
  714. 'button_name' => '帮助中心',
  715. 'status' => $status
  716. ];
  717. } else {
  718. $data['help_center'] = [
  719. 'button_name' => '帮助中心',
  720. 'status' => false
  721. ];
  722. }
  723. //隐藏爱心值插件入口
  724. $love_show = PortType::popularizeShow(\YunShop::request()->type);
  725. if (isset($data['love']) && (!$love_show)) {
  726. $data['love']['status'] = false;
  727. }
  728. //配送站
  729. if (app('plugins')->isEnabled('delivery-station')) {
  730. $data['is_open_delivery_station'] = \Setting::get('plugin.delivery_station.is_open') ? 1 : 0;
  731. } else {
  732. $data['is_open_delivery_station'] = 0;
  733. }
  734. return $data;
  735. }
  736. /**
  737. * 推广提现
  738. * @return \Illuminate\Http\JsonResponse
  739. */
  740. public static function getIncomeCount()
  741. {
  742. $amount = Income::getIncomes()->where('member_id', \YunShop::app()->getMemberId())->sum('amount');
  743. if ($amount) {
  744. return number_format($amount, 2);
  745. }
  746. return number_format(0, 2);
  747. }
  748. /**
  749. * 会员3级关系链
  750. *
  751. * @param $uid
  752. * @param string $mid
  753. * @return bool
  754. */
  755. public static function setMemberRelation($uid, $mid = '')
  756. {
  757. $curr_arr = [];
  758. $model = MemberShopInfo::getMemberShopInfo($uid);
  759. if (empty($mid)) {
  760. $mid = 0;
  761. }
  762. //生成关系3级关系链
  763. $member_model = MemberModel::getMyAgentsParentInfo($mid)->first();
  764. if (!empty($member_model)) {
  765. \Log::debug('model-生成关系3级关系链');
  766. $member_data = $member_model->toArray();
  767. $relation_str = $mid;
  768. if (!empty($member_data['yz_member'])) {
  769. $count = count($member_data['yz_member'], 1);
  770. if ($count > 3) {
  771. $relation_str .= ',' . $member_data['yz_member']['parent_id'];
  772. }
  773. if ($count > 6) {
  774. $relation_str .= ',' . $member_data['yz_member']['has_one_pre_self']['parent_id'];
  775. }
  776. }
  777. } else {
  778. $relation_str = '0';
  779. }
  780. if ($relation_str != '0') {
  781. $curr_arr = explode(',', rtrim($relation_str, ','));
  782. $res_arr = array_unique($curr_arr);
  783. if (count($res_arr) != count($curr_arr)) {
  784. return false;
  785. }
  786. if (in_array($uid, $curr_arr)) {
  787. throw new AppException('修改关系链后冲突,不能填写自己的邀请码或者下级的邀请码');
  788. }
  789. }
  790. $model->relation = $relation_str;
  791. $model->save();
  792. return $curr_arr;
  793. }
  794. public static function getOpenIdForType($member_id, $type = null)
  795. {
  796. switch ($type) {
  797. case 2:
  798. case 55:
  799. $mini_app = MemberMiniAppModel::getFansById($member_id);
  800. return $mini_app->openid;
  801. break;
  802. case 9:
  803. $mini_app = MemberWechatModel::getFansById($member_id);
  804. return $mini_app->openid;
  805. break;
  806. case 71:
  807. if (!app('plugins')->isEnabled('aggregation-cps')) {
  808. return '';
  809. }
  810. $mini_app = AggregationCpsOrderModel::where('member_id', $member_id)->first();
  811. return $mini_app->openid ?: '';
  812. break;
  813. default:
  814. $fans = McMappingFans::getFansById($member_id);
  815. return $fans->openid;
  816. }
  817. }
  818. /**
  819. * 判断用户是否关注
  820. *
  821. * @return bool
  822. */
  823. public function isFollow()
  824. {
  825. return isset($this->hasOneFans) && $this->hasOneFans->follow && !empty($this->hasOneFans->openid);
  826. }
  827. public function getMemberRole($builder)
  828. {
  829. $result = $builder;
  830. // if (app('plugins')->isEnabled('commission')) {
  831. // $result = $result->with([
  832. // 'hasOneAgent'
  833. // ]);
  834. // }
  835. //
  836. // if (app('plugins')->isEnabled('team-dividend')) {
  837. // $result = $result->with([
  838. // 'hasOneTeamDividend'
  839. // ]);
  840. // }
  841. //
  842. // if (app('plugins')->isEnabled('area-dividend')) {
  843. // $result = $result->with([
  844. // 'hasOneAreaDividend' => function ($query) {
  845. // return $query->where('status', 1);
  846. // }
  847. // ]);
  848. // }
  849. //
  850. // if (app('plugins')->isEnabled('merchant')) {
  851. // $result = $result->with([
  852. // 'hasOneMerchant',
  853. // 'hasOneMerchantCenter'
  854. // ]);
  855. // }
  856. //
  857. // if (app('plugins')->isEnabled('micro')) {
  858. // $result = $result->with([
  859. // 'hasOneMicro'
  860. // ]);
  861. // }
  862. //
  863. // if (app('plugins')->isEnabled('supplier')) {
  864. // $result = $result->with([
  865. // 'hasOneSupplier' => function ($query) {
  866. // return $query->where('status', 1);
  867. // }
  868. // ]);
  869. // }
  870. return $result;
  871. }
  872. public static function getPid()
  873. {
  874. $pid = \YunShop::request()->pid;
  875. return ($pid && ($pid != 'null' || $pid != 'undefined')) ? (int)$pid : 0;
  876. }
  877. //快递单获取会员信息
  878. public static function getMemberInfo($uid)
  879. {
  880. return self::uniacid()->find($uid);
  881. }
  882. public static function deleted($uid)
  883. {
  884. self::uniacid()
  885. ->where('uid', $uid)
  886. ->delete();
  887. }
  888. public function getAvatarImageAttribute()
  889. {
  890. return $this->avatar ? yz_tomedia($this->avatar) : yz_tomedia(\Setting::get('shop.member.headimg'));
  891. }
  892. public function getUserNameAttribute()
  893. {
  894. if (substr($this->nickname, 0, strlen('=')) === '=') {
  895. $this->nickname = ' ' . $this->nickname;
  896. }
  897. return $this->nickname;
  898. }
  899. /**
  900. * 邀请码会员
  901. *
  902. * @return null
  903. */
  904. public function getMemberIdForInviteCode()
  905. {
  906. if ($invite_code = self::hasInviteCode()) {
  907. $ids = MemberShopInfo::getMemberIdForInviteCode($invite_code);
  908. if (!is_null($ids)) {
  909. return $ids[0];
  910. }
  911. }
  912. return null;
  913. }
  914. public static function hasInviteCode()
  915. {
  916. $required = intval(\Setting::get('shop.member.required'));
  917. $invite_code = \YunShop::request()->invite_code;
  918. $is_invite = self::chkInviteCode();
  919. $member = MemberShopInfo::where('invite_code', $invite_code)->count();
  920. if ($is_invite && $required && empty($invite_code)) {
  921. return null;
  922. }
  923. if ($is_invite && isset($invite_code) && !empty($invite_code) && !empty($member)) {
  924. return $invite_code;
  925. }
  926. return null;
  927. }
  928. /**
  929. * 购物车记录
  930. * @return \Illuminate\Database\Eloquent\Relations\HasMany
  931. */
  932. public function memberCarts()
  933. {
  934. return $this->hasMany(MemberCart::class, 'uid', 'member_id');
  935. }
  936. /**
  937. * @return \Illuminate\Database\Eloquent\Relations\HasMany
  938. */
  939. public function orderGoods()
  940. {
  941. return $this->hasMany(OrderGoods::class, 'uid', 'uid');
  942. }
  943. /**
  944. * @return MemberCartCollection|mixed
  945. */
  946. public function getMemberCartCollection()
  947. {
  948. if (!isset($this->memberCartCollection)) {
  949. $this->memberCartCollection = new MemberCartCollection($this->memberCarts->all());
  950. }
  951. return $this->memberCartCollection;
  952. }
  953. /**
  954. * 邀请码是否开启
  955. *
  956. * @return int
  957. */
  958. public static function chkInviteCode()
  959. {
  960. $is_invite = intval(\Setting::get('shop.member.is_invite'));
  961. $invite_page = intval(\Setting::get('shop.member.invite_page'));
  962. // //邀请页和邀请码都开启
  963. // if (1 == $invite_page && 1 == $is_invite) {
  964. // $is_invite = 0;
  965. // }
  966. return $is_invite;
  967. }
  968. public static function memberCustomer($uid)
  969. {
  970. return self::select(['avatar', 'nickname', 'realname', 'mobile', 'credit1', 'credit2'])
  971. ->uniacid()
  972. ->where('uid', $uid);
  973. }
  974. public function hasOneAggregationCpsMember()
  975. {
  976. return $this->hasOne(MemberAggregationAppModel::class, 'member_id', 'uid');
  977. }
  978. }