SaasDistributionFrom.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\alliance\models;
  8. use app\models\Option;
  9. use app\models\SaasUser;
  10. use app\models\Store;
  11. use app\models\User;
  12. use app\models\WechatConfig;
  13. use yii\base\Model;
  14. use app\models\SaasDistribution;
  15. use app\models\BusinessRightDuration;
  16. use app\models\BusinessRightInfo;
  17. use app\utils\Wechat\WechatBusinessPay;
  18. class SaasDistributionFrom extends Model
  19. {
  20. public $store_name;
  21. public function rules()
  22. {
  23. return [
  24. [['store_name'], 'string']
  25. ];
  26. }
  27. public function getDistribution(){
  28. $saas_id = get_saas_user_id();
  29. $saas_user = get_saas_user();
  30. $distributionInfo = SaasDistribution::find()->where(['saas_id'=>$saas_id,'is_delete'=>0])->asArray()->one();
  31. $rightDurationInfo = BusinessRightDuration::find()->where(['saas_id'=>$saas_id,'is_delete'=>0])->asArray()->all();
  32. $rightInfo = $rightIds = [];
  33. $is_expire = false;
  34. $start_time = time();
  35. $end_time = time() + (86400 * 3);
  36. foreach($rightDurationInfo as $val){
  37. if($val['expire_time'] != 0 && $val['expire_time'] >= $start_time && $val['expire_time'] <= $end_time ){
  38. $is_expire = true;
  39. }
  40. if($val['expire_time'] == 0 || $val['expire_time'] >= $start_time ){
  41. $rightIds[] = $val['right_id'];
  42. }
  43. }
  44. if(!empty($rightIds)){
  45. $rightInfo = BusinessRightInfo::resetRightInfo($rightIds, 'id,name,explanation');
  46. }else{
  47. $rightInfo = [];
  48. }
  49. if(empty($distributionInfo)){
  50. $distributionInfo['platform_open_id'] = '';
  51. $distributionInfo['ali_name'] = '';
  52. $distributionInfo['name'] = '';
  53. }
  54. $distributionInfo['is_expire'] = $is_expire;
  55. $distributionInfo['right_info'] = $rightInfo;
  56. $total_wechat_bind_count = 0;
  57. $current_wechat_bind_count = 0;
  58. if (\Yii::$app->prod_is_dandianpu()) {
  59. $init_store_ids = Option::find()->where([
  60. 'name' => 'self_mini',
  61. 'value' => 1
  62. ])->select('store_id')->column();//供应链独立店铺ID
  63. $init_store_ids = Store::find()->where(['id' => $init_store_ids, 'is_delete' => 0])->select('id')->column();
  64. $total_wechat_bind_count += count($init_store_ids);
  65. $current_wechat_bind_count = User::find()->where(['store_id' => $init_store_ids, 'is_delete' => 0])
  66. ->andWhere(['<>', 'wechat_open_id', ''])->select('wechat_open_id')->count();
  67. //非独立店铺ID
  68. $storeList = Store::find()->where([
  69. 'is_delete' => 0,
  70. 'business_model' => 1,
  71. ])->andWhere(['NOT IN', 'id', $init_store_ids])->select('id')->column();
  72. if ($storeList) {
  73. $total_wechat_bind_count += count($storeList);
  74. $user = User::find()->where(['is_delete' => 0, 'binding' => $saas_user->mobile, 'store_id' => $storeList])
  75. ->andWhere(['<>', 'wechat_open_id', ''])
  76. ->select('wechat_open_id')->asArray()->one();
  77. if ($user && $user['wechat_open_id']) {
  78. $current_wechat_bind_count += count($storeList);
  79. }
  80. }
  81. }
  82. if (\Yii::$app->prod_is_shangmengduli() || \Yii::$app->prod_is_shangmengduli() || \Yii::$app->prod_is_saas()) {
  83. $storeList = Store::find()->where([
  84. 'is_delete' => 0,
  85. 'business_model' => 1
  86. ])->select('id')->column();
  87. $total_wechat_bind_count = count($storeList);
  88. $current_wechat_bind_count = User::find()->where(['store_id' => $storeList, 'is_delete' => 0, 'binding' => $saas_user->mobile])
  89. ->andWhere(['<>', 'wechat_open_id', ''])->select('wechat_open_id')->count();
  90. $storeList = Store::find()->where([
  91. 'is_delete' => 0,
  92. 'business_model' => [2, 3, 4]
  93. ])->select('id')->column();
  94. if ($storeList) {
  95. $total_wechat_bind_count += count($storeList);
  96. if (!empty($saas_user->platform_open_id)) {
  97. $current_wechat_bind_count += count($storeList);
  98. }
  99. }
  100. }
  101. $distributionInfo['total_wechat_bind_count'] = $total_wechat_bind_count;
  102. $distributionInfo['current_wechat_bind_count'] = $current_wechat_bind_count;
  103. return [
  104. 'code' => 0,
  105. 'msg' => '获取成功',
  106. 'data' => $distributionInfo
  107. ];
  108. }
  109. public function getDistributionStore() {
  110. try {
  111. $saas_user = get_saas_user();
  112. $store_name = $this->store_name;
  113. $is_dandianpu = 0;
  114. $is_dandianpu_bind = 0;
  115. //判断是否是供应链系统 且 是否是供应链小程序用户绑定过微信openid
  116. if (\Yii::$app->prod_is_dandianpu()) {
  117. $init_store_ids = Option::find()->where([
  118. 'name' => 'self_mini',
  119. 'value' => 1
  120. ])->select('store_id')->column();//供应链独立店铺ID
  121. $init_store_ids = Store::find()->where(['id' => $init_store_ids, 'is_delete' => 0])->select('id')->column();
  122. //非独立店铺ID
  123. $storeList = Store::find()->where([
  124. 'is_delete' => 0,
  125. 'business_model' => 1,
  126. ])->andWhere(['NOT IN', 'id', $init_store_ids])->select('id')->column();
  127. if ($storeList) {
  128. $user = User::find()->where(['is_delete' => 0, 'binding' => $saas_user->mobile, 'store_id' => $storeList])
  129. ->andWhere(['<>', 'wechat_open_id', ''])->select('wechat_open_id, created_at')->asArray()->one();
  130. if ($user) {
  131. $is_dandianpu_bind = 1;
  132. }
  133. }
  134. $is_dandianpu = 1;
  135. }
  136. $query = Store::find()->where(['is_delete' => 0])->select('id, name, logo, business_model')->orderBy('id desc');
  137. if (trim($store_name) && !empty($store_name)) {
  138. $query->andWhere(['like', 'name', $store_name]);
  139. }
  140. $pagination = pagination_make($query,true);
  141. foreach($pagination['list'] as &$value){
  142. $is_bind = 0;
  143. if ($is_dandianpu) {
  144. $self_mini = Option::get('self_mini', $value['id'], 'store', 0)['value'];
  145. if (intval($self_mini)) {
  146. $value['appid'] = WechatConfig::findOne(['store_id' => $value['id'], 'type' => 1])->app_id ?: '';
  147. $user = User::find()->where(['store_id' => $value['id'], 'is_delete' => 0, 'binding' => $saas_user->mobile])
  148. ->andWhere(['<>', 'wechat_open_id', ''])->select('wechat_open_id, created_at')->asArray()->one();
  149. if ($user) {
  150. $is_bind = 1;
  151. }
  152. $value['path'] = "pages/home/home";
  153. } else {
  154. $value['appid'] = Option::get('one_store_wechat_appid', 0, 'saas')['value'] ?: '';
  155. $value['path'] = "pages/home/home?store_id={$value['id']}";
  156. $is_bind = $is_dandianpu_bind;
  157. }
  158. } else {
  159. if (intval($value['business_model']) === 1) {
  160. $user = User::find()->where(['store_id' => $value['id'], 'is_delete' => 0, 'binding' => $saas_user->mobile])
  161. ->andWhere(['<>', 'wechat_open_id', ''])->select('wechat_open_id, created_at')->asArray()->one();
  162. if ($user) {
  163. $is_bind = 1;
  164. }
  165. $wechatConfig = WechatConfig::findOne(['store_id' => $value['id'], 'type' => 1]);
  166. $value['appid'] = $wechatConfig->app_id ?: '';
  167. $value['path'] = "pages/home/home";
  168. } else {
  169. $is_bind = intval($saas_user->platform_open_id);
  170. $value['appid'] = Option::get('platform_appid', 0, 'saas')['value'] ?: '';
  171. if (intval($value['business_model']) === 2) {
  172. $value['path'] = "pages/shop/shopIndex?store_id={$value['id']}";
  173. } elseif (intval($value['business_model']) === 3) {
  174. $value['path'] = "pages/shop/shopDetail?store_id={$value['id']}";
  175. } elseif (intval($value['business_model']) === 4) {
  176. $value['path'] = "alipay-order/orderMeal/orderMeal?store_id={$value['id']}";
  177. }
  178. if ($is_bind) {
  179. $value['created_at'] = date('Y-m-d H:i:s', $saas_user->created_at);
  180. }
  181. }
  182. }
  183. $value['created_at'] = isset($user) ? date('Y-m-d H:i:s', $user['created_at']) : ($value['created_at'] ?: '');
  184. $value['is_bind'] = $is_bind;
  185. }
  186. $pagination['user_info'] = [
  187. 'name' => $saas_user->name,
  188. 'avatar' => $saas_user->avatar,
  189. 'level_name' => '店铺会员'
  190. ];
  191. return [
  192. 'code' => 0,
  193. 'msg' => '获取成功',
  194. 'data' => $pagination
  195. ];
  196. } catch (\Exception $e) {
  197. return [
  198. 'code' => 1,
  199. 'msg' => $e->getMessage(),
  200. ];
  201. }
  202. }
  203. public function wechatDistribution($code,$open_id){
  204. if(empty($open_id) && empty($code)){
  205. return [
  206. 'code' => 1,
  207. 'msg' => '参数不能为空',
  208. ];
  209. }
  210. $type = 0;
  211. if(empty($open_id)){
  212. $wechat_mini = WechatBusinessPay::BussinessWechatMini();
  213. $mini_res = $wechat_mini->auth->session($code);
  214. if(!isset($mini_res['openid'])){
  215. return [
  216. 'code' => 1,
  217. 'msg' => '获取微信息失败',
  218. 'res' => $mini_res,
  219. ];
  220. }
  221. $open_id = $mini_res['openid'];
  222. $type = 1; //小程序获取
  223. }else{
  224. $type = 2; //app获取
  225. }
  226. $saas_id = get_saas_user_id();
  227. $distributionInfo = SaasDistribution::findOne(['saas_id'=>$saas_id]);
  228. if(!$distributionInfo){
  229. $distributionInfo = new SaasDistribution();
  230. $distributionInfo->created_at = time();
  231. }
  232. $distributionInfo->wechat_type = $type;
  233. $distributionInfo->saas_id = $saas_id;
  234. $distributionInfo->platform_open_id = $open_id;
  235. $distributionInfo->bank = '';
  236. if($distributionInfo->save() ){
  237. return [
  238. 'code' => 0,
  239. 'msg' => '设置成功'
  240. ];
  241. }else{
  242. return [
  243. 'code' => 1,
  244. 'msg' => '设置失败'
  245. ];
  246. }
  247. }
  248. public function aliDistribution($name,$ali_name){
  249. $saas_id = get_saas_user_id();
  250. $distributionInfo = SaasDistribution::findOne(['saas_id'=>$saas_id]);
  251. if(!$distributionInfo){
  252. $distributionInfo = new SaasDistribution();
  253. $distributionInfo->created_at = time();
  254. }
  255. $distributionInfo->saas_id = $saas_id;
  256. $distributionInfo->name = $name;
  257. $distributionInfo->ali_name = $ali_name;
  258. $distributionInfo->bank = '';
  259. if($distributionInfo->save() ){
  260. return [
  261. 'code' => 0,
  262. 'msg' => '设置成功'
  263. ];
  264. }else{
  265. return [
  266. 'code' => 1,
  267. 'msg' => $distributionInfo->errors
  268. ];
  269. }
  270. }
  271. public function bankDistribution($saas_id, $conf){
  272. if(is_string($conf)){
  273. $conf = json_decode($conf, true);
  274. }
  275. if(!$conf || empty($conf)){
  276. return [
  277. 'code' => 1,
  278. 'msg' => '参数错误',
  279. ];
  280. }
  281. $distributionInfo = SaasDistribution::findOne(['saas_id'=>$saas_id]);
  282. if(!$distributionInfo){
  283. $distributionInfo = new SaasDistribution();
  284. $distributionInfo->created_at = time();
  285. }
  286. $distributionInfo->saas_id = $saas_id;
  287. $distributionInfo->bank = json_encode($conf);
  288. if($distributionInfo->save() ){
  289. return [
  290. 'code' => 0,
  291. 'msg' => '设置成功'
  292. ];
  293. }else{
  294. return [
  295. 'code' => 1,
  296. 'msg' => $distributionInfo->errors
  297. ];
  298. }
  299. }
  300. }